Let's start by getting rid of the debounce. It will work, but it will act funny without it, but it's much easier to write. So let's start there. Likewise, get rid of the waiting (since there's no debounce, no need to wait).
01 | script.Parent.Touched:connect( function (hit) |
02 | local h = hit.Parent:FindFirstChild( "Humanoid" ) |
04 | if h.WalkSpeed = = 16 then |
08 | if h.Walkspeed = = 200 then |
So notice what your if-else pair is doing: If h
isn't nil
, we check that it's speed is 16. Else, we check if it's speed is 200. Except that can't be right - the else
clause will always error since we can't get the Walkspeed of nil.
So your else
is attached to the wrong thing. Let's fix that:
01 | script.Parent.Touched:connect( function (hit) |
02 | local h = hit.Parent:FindFirstChild( "Humanoid" ) |
04 | if h.WalkSpeed = = 16 then |
06 | elseif h.WalkSpeed = = 200 then |
I combined the else if
into an elseif
... This is very helpful, because otherwise we'd require an additional level of indentation (and another end
). I also changed the 20 to a 16, since I assume that's what you wanted.
This script will pretty much work. The problem is, most of the time when you touch the part, you'll touch is a bunch of times in rapid succession. As a consequence, it will be hard to control what speed you end up with - touch an even number of times and there's no change, touch an odd number and there will be.
So we want to add a debounce to this so that it will only handle one touch ever 0.25 seconds, for example. So we need a variable (which we'll call debounce
) to allow this.
Start by saying debounce = true
. So our code is:
02 | script.Parent.Touched:connect( function (hit) |
03 | local h = hit.Parent:FindFirstChild( "Humanoid" ) |
05 | if h.WalkSpeed = = 16 then |
07 | elseif h.WalkSpeed = = 200 then |
So first, we want to make sure that we only do something inside this function when debounce
is true. If it's false, we do nothing... So that means we'll need another if
:
02 | script.Parent.Touched:connect( function (hit) |
03 | local h = hit.Parent:FindFirstChild( "Humanoid" ) |
05 | if debounce = = true then |
06 | if h.WalkSpeed = = 16 then |
08 | elseif h.WalkSpeed = = 200 then |
However, saying debounce == true
is the same thing as just saying debounce
, so let's write that instead:
02 | script.Parent.Touched:connect( function (hit) |
03 | local h = hit.Parent:FindFirstChild( "Humanoid" ) |
06 | if h.WalkSpeed = = 16 then |
08 | elseif h.WalkSpeed = = 200 then |
Great! But right now it does the same thing as when we had no debounce. So what we need to do is turn the debounce off once it passes:
02 | script.Parent.Touched:connect( function (hit) |
03 | local h = hit.Parent:FindFirstChild( "Humanoid" ) |
07 | if h.WalkSpeed = = 16 then |
09 | elseif h.WalkSpeed = = 200 then |
And now, you'll be able to touch it once... and never again. Not exactly what we wanted, but it's a step in the right direction.
So what we do is, whenever you press it successfully, we do whatever we needed to. 0.25 seconds later, we set debounce
back to true, so that the next time something hits it, it succeeds.
02 | script.Parent.Touched:connect( function (hit) |
03 | local h = hit.Parent:FindFirstChild( "Humanoid" ) |
07 | if h.WalkSpeed = = 16 then |
09 | elseif h.WalkSpeed = = 200 then |
Then your script should work as intended.