Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What is the problem with my script? [SOLVED]

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

✓ Accepted answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.