I am trying to make an obby, where you have to complete a series of parkour stuff.
And you have to touch an obby so my text GUI will say something
Problem
The problem is that it can’t find my part.
Here is the script
local Timer = script.Parent
local timevalue = 20
local part = game.Workspace.Complete
local function TimerStart()
for i = 1, 20 do
wait(1)
timevalue = timevalue - 1
Timer.Text = timevalue
end
Timer.Text = "You have failed to complete this trial."
end
local function onTouched(part)
local humanoid = part.Parent:FindFirstChild("Humanoid")
if humanoid and timevalue > 0 then
Timer.Text = "You have completed the obby!"
timevalue = 0
end
end
while wait(1) do
TimerStart()
end
script.Parent.Touched:Connect(onTouched)
It’s because you put your while true do loop before your event, therefore the function is never connected to it because it’s waiting for the loop to end.
Fixed code:
--//Services
local Players = game:GetService("Players")
--//Variables
local LocalPlayer = Players.LocalPlayer
local Timer = script.Parent
local completePart = workspace.Complete
--//Controls
local timeValue = 20
local hasFinished = false
--//Functions
local function TimerStart()
hasFinished = false
timeValue = 20
while timeValue >= 0 and not hasFinished do
task.wait(1)
timeValue -= 1
Timer.Text = timeValue
end
if hasFinished then
Timer.Text = "You have completed the obby!"
else
Timer.Text = "You have failed to complete this trial."
end
end
local function onTouched(part)
local player = Players:GetPlayerFromCharacter(part.Parent)
if not player or player ~= LocalPlayer or timeValue <= 0 then
return
end
hasFinished = true
timeValue = 0
end
completePart.Touched:Connect(onTouched)
while task.wait(1) do
TimerStart()
end
I also made added a check in the .Touched function so that it will only say “You have completed the obby” if you were the one to complete it. (and not if someone else completes it)
--//Services
local Players = game:GetService("Players")
--//Variables
local LocalPlayer = Players.LocalPlayer
local Timer = script.Parent
local completePart = workspace:WaitForChild("Complete")
--//Controls
local timeValue = 20
local hasFinished = false
--//Functions
local function TimerStart()
hasFinished = false
timeValue = 20
while timeValue >= 0 and not hasFinished do
task.wait(1)
timeValue -= 1
Timer.Text = timeValue
end
if hasFinished then
Timer.Text = "You have completed the obby!"
else
Timer.Text = "You have failed to complete this trial."
end
end
local function onTouched(part)
local player = Players:GetPlayerFromCharacter(part.Parent)
if not player or player ~= LocalPlayer or timeValue <= 0 then
return
end
hasFinished = true
timeValue = 0
end
completePart.Touched:Connect(onTouched)
while task.wait(1) do
TimerStart()
end