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

What is the problem with my script? [SOLVED]

Asked by
LAC44 20
9 years ago

My script is supposed to make my walkspeed 45 when pressing e, and to change the walkspeed back to normal when pressing e again, but it only makes the walkspeed change to 45, it doesn't change back to 16 when I press e again.

Here is the script:

plr = game.Players.LocalPlayer
mouse = plr:GetMouse()

mouse.KeyDown:connect(function(key)
    if key == "e" then
        plr.Character.Humanoid.WalkSpeed = 45
    elseif key == "e" and plr.Character.Humanoid.WalkSpeed == 45 then
        plr.Character.Humanoid.WalkSpeed = 16
    end
end)

1 answer

Log in to vote
1
Answered by 9 years ago

The reason is because since the first condition will be true when the player hits 'e', it skips the other conditional statements. What you can easily do to fix this is do the following:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

mouse.KeyDown:connect(function(key)
    if key == "e" and plr.Character.Humanoid.WalkSpeed == 16 then
        plr.Character.Humanoid.WalkSpeed = 45
    elseif key == "e" and plr.Character.Humanoid.WalkSpeed == 45 then
        plr.Character.Humanoid.WalkSpeed = 16
    end
end)

Or you can even add a form of debounce:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local sanic = false
mouse.KeyDown:connect(function(key)
    if not sanic and key == "e" then
        plr.Character.Humanoid.WalkSpeed = 45;
        sanic = true;
    elseif sanic and key == "e"  then
        plr.Character.Humanoid.WalkSpeed = 16;
        sanic = false;
    end
end)
Ad

Answer this question