Conveyor Belts
Lesson 7 of 18 in Coddy's Roblox Game: Endless Tower course.
The Conveyor stage has a dark belt, tagged Conveyor, that pushes you back the way you came. An anchored part never moves, but give it an AssemblyLinearVelocity and whatever stands on it is carried along, as on a real belt.
Which way? Every part has a front, and part.CFrame.LookVector is a Vector3 one stud long pointing out of it. The belt was turned so its front faces back down the walkway:
part.AssemblyLinearVelocity = part.CFrame.LookVector * Settings.BeltSpeedUsing the part's own direction means a builder can turn the belt to face any way and it still works, with no change to the script.
Challenge
MediumIn setupStage, give every part tagged Conveyor an AssemblyLinearVelocity of its CFrame.LookVector times Settings.BeltSpeed.
Press Play and walk against the belt. Then Check.
Try it yourself
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")
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
-- A vanishing step fades soon after someone lands on it, then comes back
local function makeVanish(part)
part.Touched:Connect(function(hit)
if part:GetAttribute("Gone") or not hit.Parent:FindFirstChild("Humanoid") then
return
end
part:SetAttribute("Gone", true)
task.wait(Settings.VanishDelay)
part.Transparency = 0.8
part.CanCollide = false
task.wait(Settings.VanishTime)
part.Transparency = 0
part.CanCollide = true
part:SetAttribute("Gone", nil)
end)
end
-- Spinners turn a little every frame
RunService.Heartbeat:Connect(function(dt)
for _, spinner in CollectionService:GetTagged("Spinner") do
if spinner:IsDescendantOf(workspace) then
spinner:PivotTo(spinner:GetPivot() * CFrame.Angles(0, math.rad(Settings.SpinSpeed) * dt, 0))
end
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
if part:HasTag("Vanish") then
makeVanish(part)
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()
This lesson includes a short quiz. Start the lesson to answer it and track your progress.