Hello! I’ve been learning LUA in Roblox Education. When im learning loops and elseif, I got stuck in the optional challenge.
https://education.roblox.com/en-us/resources/intro-to-coding-coding-3-multiple-conditions-with-if-elseif-and-else
The problem is that I think the raceActive boolean does change to true but the timer (while true loop) doesn’t work.
Here’s my code :
local timePassed = 0
local finishLine = script.Parent
local startL = game.Workspace.StartLine
-- Used to keep finish() and timer from repeating when race is over
local raceActive = true
-- Runs when the player touches the finish line and shows them an award
local function finish()
raceActive = false
print("You finished in " .. timePassed)
if timePassed <= 10 then
print("You get a gold medal!")
elseif timePassed > 10 and timePassed <= 20 then
print("You get a silver medal!")
elseif timePassed > 20 and timePassed <= 30 then
print("You get a bronze medal!")
else
print("Try again!")
end
end
-- Checks if a player touches the part when a race is active
local function partTouched(otherPart)
local character = otherPart.Parent
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
if humanoid and raceActive == true then
finish()
end
end
finishLine.Touched:Connect(partTouched)
local function star()
raceActive = true
end
local function partTouched2(otherPart)
local character = otherPart.Parent
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
if humanoid and raceActive == false then
star()
end
end
startL.Touched:Connect(partTouched2)
-- Keeps track of race time while the race is active. Needs to be at script bottom.
while raceActive == true do
print("Activated")
wait(1)
timePassed = timePassed + 1
print(timePassed)
end
Thanks!