Treadmill Bug (Explained in Post)

Hello, Im currently trying to make a simple Treadmill System where every 5 seconds on the treadmill, the players WalkSpeed increases by 0.02 (Currently 1 to see the speed change). Currently, my script is having a issue where it waits 5 seconds and then it gives the player the speed, HOWEVER, it starts giving the player +1 WalkSpeed Rapidly even Off the treadmill.

Script:

local part = script.Parent

part.Touched:Connect(function(hit)
while task.wait(5) do
	
	local character = hit.Parent
	if character then
		local humanoid = character:FindFirstChild("Humanoid")
		    if humanoid then
			    humanoid.WalkSpeed += 1
		end
	end
  end
end)

If you have a solution, please tell me, I’d love to know how to fix it!

You need to add debounce to prevent the .Touched event from firing multiple times in a row.

Also, I believe there is something like a .TouchEnded event, which you can implement to check when tje player is not touching the part anymore.

local part = script.Parent

local debounceCharacters = {}
part.Touched:Connect(function(hit)
	local character = hit.Parent
	if character then
		local humanoid = character:FindFirstChild("Humanoid")
		    if humanoid then
if debounceCharacters[character] then return end
debounceCharacters[character] = true
			    humanoid.WalkSpeed += 1
task.delay(5, function() debounceCharacters = false end)
		end
	end
end)

Your script is outputting an error.

Script:9: attempt to index boolean with Instance

Apologies for the oversight.

local part = script.Parent

local debounceCharacters = {}
part.Touched:Connect(function(hit)
	local character = hit.Parent
	if character then
		local humanoid = character:FindFirstChild("Humanoid")
		    if humanoid then
if debounceCharacters[character] then return end
debounceCharacters[character] = true
			    humanoid.WalkSpeed += 1
task.delay(5, function() debounceCharacters[character] = false end)
		end
	end
end)

this works as intended, but is their a way to make it so it resets when the player goes off of the part? If not then ill label this script as the solution

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.