Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Wait Until Touch Ended? [HELP]

Asked by 7 years ago

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)
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). GoldenPhysics 474 — 7y
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. GatitosMansion 187 — 7y
0
Try my script again, I have edited the problem out. TheDeadlyPanther 2460 — 7y

1 answer

Log in to vote
2
Answered by 7 years ago
Edited 7 years ago

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

0
I copied your script and replaced it with mine, sorry it did not work for me :c GatitosMansion 187 — 7y
0
What was the error in the output? And what didn't work about it? TheDeadlyPanther 2460 — 7y
0
You actually added and end. He has and end on line 4 that goes with the if on the same line. GoldenPhysics 474 — 7y
0
Oh, woops. I'll edit that out. TheDeadlyPanther 2460 — 7y
0
The problem was in line 09 it said "Walkspeed" it was suppose to be "WalkSpeed". It works now. Thanks! GatitosMansion 187 — 7y
Ad

Answer this question