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

Can anyone help me with this broken Speed Giver Function?

Asked by 9 years ago

I tried making a brick that would look and see if you had a speed of 16, then change it to 200, or if you had a speed of 200, then change it to a speed of 16. Can anyone help me fix my errors? (Im new at scripting)

01debounce = true
02 
03script.Parent.Touched:connect(function(hit)
04    local h = hit.Parent:FindFirstChild("Humanoid")
05    if (h ~= nil and debounce == true) then
06    debounce = false
07        if h.WalkSpeed == 16 then
08           h.WalkSpeed = 200
09           wait(.25)
10        end
11    else
12        if h.Walkspeed == 200 then
13           h.Walkspeed = 20
14        end
15    debounce = true 
16    end
17end)

2 answers

Log in to vote
0
Answered by
TaslemGuy 211 Moderation Voter
9 years ago

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).

01script.Parent.Touched:connect(function(hit)
02    local h = hit.Parent:FindFirstChild("Humanoid")
03    if h ~= nil then
04        if h.WalkSpeed == 16 then
05            h.WalkSpeed = 200
06        end
07    else
08        if h.Walkspeed == 200 then
09            h.Walkspeed = 20
10        end
11    end
12end)

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:

01script.Parent.Touched:connect(function(hit)
02    local h = hit.Parent:FindFirstChild("Humanoid")
03    if h ~= nil then
04        if h.WalkSpeed == 16 then
05            h.WalkSpeed = 200
06        elseif h.WalkSpeed == 200 then
07            h.WalkSpeed = 16
08        end
09    end
10end)

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:

01debounce = true
02script.Parent.Touched:connect(function(hit)
03    local h = hit.Parent:FindFirstChild("Humanoid")
04    if h ~= nil then
05        if h.WalkSpeed == 16 then
06            h.WalkSpeed = 200
07        elseif h.WalkSpeed == 200 then
08            h.WalkSpeed = 16
09        end
10    end
11end)

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:

01debounce = true
02script.Parent.Touched:connect(function(hit)
03    local h = hit.Parent:FindFirstChild("Humanoid")
04    if h ~= nil then
05        if debounce == true then
06            if h.WalkSpeed == 16 then
07                h.WalkSpeed = 200
08            elseif h.WalkSpeed == 200 then
09                h.WalkSpeed = 16
10            end
11        end
12    end
13end)

However, saying debounce == true is the same thing as just saying debounce, so let's write that instead:

01debounce = true
02script.Parent.Touched:connect(function(hit)
03    local h = hit.Parent:FindFirstChild("Humanoid")
04    if h ~= nil then
05        if debounce then
06            if h.WalkSpeed == 16 then
07                h.WalkSpeed = 200
08            elseif h.WalkSpeed == 200 then
09                h.WalkSpeed = 16
10            end
11        end
12    end
13end)

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:

01debounce = true
02script.Parent.Touched:connect(function(hit)
03    local h = hit.Parent:FindFirstChild("Humanoid")
04    if h ~= nil then
05        if debounce then
06            debounce = false -- I added this line
07            if h.WalkSpeed == 16 then
08                h.WalkSpeed = 200
09            elseif h.WalkSpeed == 200 then
10                h.WalkSpeed = 16
11            end
12        end
13    end
14end)

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.

01debounce = true
02script.Parent.Touched:connect(function(hit)
03    local h = hit.Parent:FindFirstChild("Humanoid")
04    if h ~= nil then
05        if debounce then
06            debounce = false
07            if h.WalkSpeed == 16 then
08                h.WalkSpeed = 200
09            elseif h.WalkSpeed == 200 then
10                h.WalkSpeed = 16
11            end
12            wait( 0.25 ) -- wait for a bit before we reset debounce
13            debounce = true
14        end
15    end
16end)

Then your script should work as intended.

0
It didn't work, idk why, it says there is no errors in output. LordZerefu 30 — 9y
0
I forgot to include the debounce initialization in the snippets. TaslemGuy 211 — 9y
Ad
Log in to vote
0
Answered by 5 years ago

Try this.

1script.Parent.Touched:Connect(function(hit)
2    if hit.Parent:FindFirstChild("Humanoid") then
3        hit.Parent.Humanoid.WalkSpeed = 200
4      end
5end)

Answer this question