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

KeyDown Second Time?

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

✓ Accepted answer

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

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