Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help with Speed decay block?!

In the game I am creating, a part of it will be to find objects that help speed (Which I was able to script) up the player, while over time their speed decrease (Which I need help with)

I am very new to scripting and can't seem to find any scripting problems that involve decay of speed from touching a block.

What I want to achieve is to create a script that allows for the Walkspeed to decrease by 1 every 3 seconds, until it reaches 0, I have tried the script below, and although it slows me down, it does it too quickly.

TLDR: I need a script that when touched once, reduces the players speed by 1 every 3 seconds. The one I currently have (below) registers all parts of a player that hit the block, and slows them down almost instantly.

script.Parent.Touched:Connect(function(hit)
	repeat
		hit.Parent.Humanoid.WalkSpeed = hit.Parent.Humanoid.WalkSpeed - 1
		wait (3)
	until hit.Parent.Humanoid.WalkSpeed == 0
end)

1 answer

✓ Accepted answer

Everytime you use touch you should want to use a debounce, it is to prevent the touch firing multiple times at once.

local debounce = true
local SecsUntilNextTouchPossible = 1
script.Parent.Touched:Connect(function(hit)
	if debounce then
	debounce = false
	delay(SecsUntilNextTouchPossible, function() debounce = true end)
	repeat
		hit.Parent.Humanoid.WalkSpeed = hit.Parent.Humanoid.WalkSpeed - 1
		wait(3)
	until hit.Parent.Humanoid.WalkSpeed == 0
	end
end)

But, you know, this way, what if the part touches an accessory instead of a body part? It would error when it tried to get Humanoid from Handle's Parent, that is the Accessory. Fix it by making it work only with body part:

local debounce = true
local SecsUntilNextTouchPossible = 1
script.Parent.Touched:Connect(function(hit)
	if debounce and hit.Parent:FindFirstChild("Humanoid") then
	debounce = false
	delay(SecsUntilNextTouchPossible, function() debounce = true end)
	repeat
		hit.Parent.Humanoid.WalkSpeed = hit.Parent.Humanoid.WalkSpeed - 1
		wait(3)
	until hit.Parent.Humanoid.WalkSpeed == 0
	end
end)

1 comment

0

This makes so much more sense, thank you! I kept on seeing debounce in the scripts I was looking at and passed it off as a variable instead of what its actual function was, TY!!!!!!

This discussion is preserved as it was. New answers, comments, and votes are closed.