So I started working on a racing game, and I wanted to add a stopwatch for the player to see how long it took them to go over the track. This is the script:
local stopwatch = 0 function stopWatchstart() while true do stopwatch = stopwatch + 1 wait(1) script.Parent.Text = (stopwatch.."") end end game.Workspace.StartStopwatch.Touched:Connect(function() stopWatchstart() end)
The problem is (I think its the problem, correct me if I'm wrong) that it starts the timer and adds massive amounts of seconds to the textlabel. How do I detect that the player that goes through the invisible part (startstopwatch) and not a random part?
What you need is debounce. Here is how it works.
local stopwatch = 0 local Debounce = false function stopWatchstart() while true do stopwatch = stopwatch + 1 wait(1) script.Parent.Text = (stopwatch.."") end end game.Workspace.StartStopwatch.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then if Debounce == false then Debounce = true stopWatchstart() wait(5) Debounce = false end end end)
What this does is first it checks if the player that touched is actually a player (i.e it has a humanoid). Then we also make a debounce for the problem that you are saying that it is adding lots of seconds to the textLabel.
Hope this helps