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)

debounce = true

script.Parent.Touched:connect(function(hit)
    local h = hit.Parent:FindFirstChild("Humanoid")
    if (h ~= nil and debounce == true) then
    debounce = false
        if h.WalkSpeed == 16 then
           h.WalkSpeed = 200
           wait(.25)
        end
    else 
        if h.Walkspeed == 200 then
           h.Walkspeed = 20
        end 
    debounce = true  
    end
end)

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

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 -- I added this line
            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 ) -- wait for a bit before we reset debounce
            debounce = true
        end
    end
end)

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.

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        hit.Parent.Humanoid.WalkSpeed = 200
      end
end)

Answer this question