So, I want the script to act this way; when the player touched the part, his movement speed is decreased by 4 until the player stops touching the block. When the player stops touching the block his speed goes back to normal. Do you have any idea how to make this happen? Thanks!
local debounce = false function onTouched(hit) if debounce == true then return end debounce = true hit.Parent.Humanoid.WalkSpeed = hit.Parent.Humanoid.WalkSpeed - 4 repeat until script.Parent.TouchEnded:connect(onTouched) hit.Parent.Humanoid.Walkspeed = hit.Parent.Humanoid.WalkSpeed + 4 debounce = false end script.Parent.Touched:connect(onTouched)
Here's what you're doing wrong:
1.
repeat until
is the same as while true do
- it will crash the user because it is repeating endlessly without waiting.
As well as that, just remove just remove Line 7
. Repeat loops aren't a very good way to check for events.
2.
Try obj.EventName:wait()
Change Line 8
to script.Parent.TouchEnded:wait()
.
This should make your code work.
This should be the end result:
local debounce = false function onTouched(hit) if debounce == true then return end debounce = true hit.Parent.Humanoid.WalkSpeed = hit.Parent.Humanoid.WalkSpeed - 4 script.Parent.TouchEnded:wait() -- what we changed hit.Parent.Humanoid.Walkspeed = hit.Parent.Humanoid.WalkSpeed + 4 debounce = false end script.Parent.Touched:connect(onTouched)
TIP:
Use Tab
to format your code, not space, as it is very inconsistent.
Hope I helped! If I got anything wrong, be sure to tell me, and if you need help, again, please tell me.
~TDP