Menu
CoddyTech

The Spinner

Lesson 5 of 18 in Coddy's Roblox Game: Endless Tower course.

The Spinner stage has a red arm that sweeps across the walkway. It is a Model, tagged Spinner, whose pivot is its hub. Turning the whole Model a little every frame makes it spin.

RunService.Heartbeat fires once every frame and passes dt, the seconds since the last frame (about 1/60). Turning by speed * dt each frame adds up to speed every second, however fast the game runs.

A CFrame is a position plus a rotation. Multiplying by CFrame.Angles turns it, and the angles are in radians, so convert degrees with math.rad:

spinner:PivotTo(spinner:GetPivot() * CFrame.Angles(0, math.rad(90) * dt, 0))

CollectionService:GetTagged("Spinner") lists everything carrying the tag, including the templates in ServerStorage, so only turn the ones in the workspace. The arm is tagged Kill, so it already ends a life. In this lesson every stage is a Spinner.

challenge icon

Challenge

Medium
  1. Near the top, get CollectionService and RunService with game:GetService.
  2. Connect RunService.Heartbeat. Each frame, for every Model tagged Spinner that is in the workspace, pivot it by CFrame.Angles(0, math.rad(Settings.SpinSpeed) * dt, 0).

Press Play and time your run past the arm, or jump it. Then Check.

Try it yourself

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)

local stages = ServerStorage.Stages
local tower = nil

print("Each tower has " .. Settings.StageCount .. " stages")

-- Every player gets Wins on the leaderboard
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Parent = leaderstats
end)

-- Anything tagged Kill ends a life
local function killOnTouch(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		humanoid.Health = 0
	end
end

-- Turn on every obstacle in a stage, by its tags
local function setupStage(stage)
	for _, part in stage:GetDescendants() do
		if part:HasTag("Kill") then
			part.Touched:Connect(killOnTouch)
		end
	end
end

-- Any stage, at random
local function pickStage()
	local all = stages:GetChildren()
	return all[math.random(1, #all)]
end

-- Put a copy of a model on top of the tower; returns the copy and the new top
local function stack(template, bottom)
	local copy = template:Clone()
	local box, size = copy:GetBoundingBox()
	local lift = bottom - (box.Position.Y - size.Y / 2)
	copy:PivotTo(copy:GetPivot() + Vector3.new(0, lift, 0))
	copy.Parent = tower
	return copy, bottom + size.Y
end

-- Build a new random tower, bottom up
local function buildTower()
	tower = Instance.new("Model")
	tower.Name = "Tower"
	tower.Parent = workspace
	local top = 0
	for i = 1, Settings.StageCount do
		local stage, newTop = stack(pickStage(), top)
		top = newTop
		setupStage(stage)
	end
	stack(ServerStorage.Finish, top)
end

buildTower()
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Roblox Game: Endless Tower