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

KeyDown Second Time?

Asked by 8 years ago

Could you help with this? It currently just makes the walkspeed 0 when x is pressed.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local character = Player.Character

function speed(speed)
    character.Humanoid.WalkSpeed = speed
end


Mouse.KeyDown:connect(function(Key)
if Key == "x" then 
    speed(0)
end
end)

How can I make this so if the player presses x a second time the speed would go back to 16? I'm not sure how to state if the key is pressed again.

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

Think about what you want the key event to end up doing:

  • IF they were walking, stop
  • IF they were stopped, start walking

The "stop" part is just speed(0) and the "start walking" is just speed(16).

The "if they were walking" just needs to check their current walk speed:

    if Key == "x" then
        if character.Humanoid.WalkSpeed > 0 then
            speed(0)
        else
            speed(16)
        end
    end
Ad

Answer this question