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

Wait Until Touch Ended? [HELP]

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)

3 comments

0

First, take out line 7 entirely. Second, replace "connect" on line 8 with "wait" and take out onTouched (and try "Wait", I don't know if it needs to be capitalized).

0

Sorry it didn't work for me. I removed line 7, and for line 8 I replaced "connect" with "wait". Also I removed "onTouched" on line 8.

0

Try my script again, I have edited the problem out.

1 answer

✓ Accepted answer

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

5 comments

0

I copied your script and replaced it with mine, sorry it did not work for me :c

0

What was the error in the output? And what didn't work about it?

0

You actually added and end. He has and end on line 4 that goes with the if on the same line.

0

Oh, woops. I'll edit that out.

0

The problem was in line 09 it said "Walkspeed" it was suppose to be "WalkSpeed". It works now. Thanks!

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