Finish Line
Part of the Roblox Game Scripting section of Coddy's Lua journey. Lesson 24 of 25.
A finish line counts a win and sends the winner back to the start. player:LoadCharacterAsync() gives the player a fresh character on their spawn, without waiting for the respawn timer.
It takes a moment. Until the new character arrives, the finish keeps firing Touched for the old character's other body parts, and every one of them would count a win. A debounce stops that, and a finish needs one per player, so two players finishing together both count. Keep it in a table with the player as the key:
local busy = {}
if not busy[player] then
busy[player] = true
-- count the win, send the player back
task.wait(1)
busy[player] = nil
endbusy[player] reads or sets the table's entry for that player, and a missing entry is nil. busy[player] = nil removes it, so their next crossing counts again.
Challenge
MediumCount a win each time a player reaches the gold finish pad. The per-player debounce is ready.
- Add 1 to the player's
Wins. - Send them back to the start with
player:LoadCharacterAsync(). - After
task.wait(1), clear their debounce inbusy, so their next run counts.
Press Play and run to the finish twice: Wins shows 2. Then Check.
Try it yourself
local Players = game:GetService("Players")
local finish = script.Parent
local busy = {}
finish.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player and not busy[player] then
busy[player] = true
-- 1. Add 1 to the player's Wins
-- 2. Send them back to the start
-- 3. After 1 second, let them win again
end
end)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Roblox Game Scripting
2Loops and Functions
Row of PillarsStaircase BuilderTower BuilderCoin FactoryBlinking LightVanishing Bridge5Your First Game
Your First ObbyPractice on your own: Online Lua compiler