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).
script.Parent.Touched:connect(function(hit)
local h = hit.Parent:FindFirstChild("Humanoid")
if h ~= nil then
if h.WalkSpeed == 16 then
h.WalkSpeed = 200
end
else
if h.Walkspeed == 200 then
h.Walkspeed = 20
end
end
end)
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:
script.Parent.Touched:connect(function(hit)
local h = hit.Parent:FindFirstChild("Humanoid")
if h ~= nil then
if h.WalkSpeed == 16 then
h.WalkSpeed = 200
elseif h.WalkSpeed == 200 then
h.WalkSpeed = 16
end
end
end)
I combined the else ifinto 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:
debounce = true
script.Parent.Touched:connect(function(hit)
local h = hit.Parent:FindFirstChild("Humanoid")
if h ~= nil then
if h.WalkSpeed == 16 then
h.WalkSpeed = 200
elseif h.WalkSpeed == 200 then
h.WalkSpeed = 16
end
end
end)
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:
debounce = true
script.Parent.Touched:connect(function(hit)
local h = hit.Parent:FindFirstChild("Humanoid")
if h ~= nil then
if debounce == true then
if h.WalkSpeed == 16 then
h.WalkSpeed = 200
elseif h.WalkSpeed == 200 then
h.WalkSpeed = 16
end
end
end
end)
However, saying debounce == true is the same thing as just saying debounce, so let's write that instead:
debounce = true
script.Parent.Touched:connect(function(hit)
local h = hit.Parent:FindFirstChild("Humanoid")
if h ~= nil then
if debounce then
if h.WalkSpeed == 16 then
h.WalkSpeed = 200
elseif h.WalkSpeed == 200 then
h.WalkSpeed = 16
end
end
end
end)
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:
debounce = true
script.Parent.Touched:connect(function(hit)
local h = hit.Parent:FindFirstChild("Humanoid")
if h ~= nil then
if debounce then
debounce = false
if h.WalkSpeed == 16 then
h.WalkSpeed = 200
elseif h.WalkSpeed == 200 then
h.WalkSpeed = 16
end
end
end
end)
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.
debounce = true
script.Parent.Touched:connect(function(hit)
local h = hit.Parent:FindFirstChild("Humanoid")
if h ~= nil then
if debounce then
debounce = false
if h.WalkSpeed == 16 then
h.WalkSpeed = 200
elseif h.WalkSpeed == 200 then
h.WalkSpeed = 16
end
wait( 0.25 )
debounce = true
end
end
end)
Then your script should work as intended.
2 comments
It didn't work, idk why, it says there is no errors in output.
I forgot to include the debounce initialization in the snippets.