Finish Line
Part of the Roblox Game Scripting section of Coddy's Lua journey. Lesson 23 of 24.
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
MediumThe gold pad is the finish, and this script is inside it. Every player already has leaderstats with Wins at 0, and the per-player debounce is set up.
- Add 1 to the player's
Wins. - Send them back to the start with
player:LoadCharacterAsync(). - Then clear their debounce, so their next run counts.
Press Play, run to the finish twice, 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