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

Disabling WASD/Arrow Key Movements?

Hi I'm creating sort of a 2D Endless Runner game, and I'm using this method... : http://wiki.roblox.com/index.php?title=Controlling_a_Player%27s_Character to make the player move on its own. However, if you use WASD or Arrow keys, that cancels the movement, and removing all control no longer allows you to jump. Is there a way to remove all WASD/Arrow Keys control while still being able to jump?

1 comment

0

Please look at both answers! Both Goulstem and I worked hard to make these answers!

2 answers

✓ Accepted answer

When you use this method of preventing someone from controlling, they cannot jump, but they are still able to jump. You can make them jump when they press a key, you can detect when they press a key though KeyDown or UnserInputService.



Making a player jump

There is a way to force a player to jump, In the humanoid there is a Jump property which is a boolean. If it is true, the player jumps once.

workspace.Player.Humanoid.Jump = true

Fun Facts about Jump property

  • You can make an anti-jump script using the changed event:
workspace.Player.Humanoid.Changed:connect(function(jump)
	if jump == "Jump" then
		workspace.Player.Humanoid.Jump = false
	end
end)


KeyDown

It's deprecated, you shouldn't use it.

--LocalScript
local plyr = game:GetService("Players").LocalPlayer
local mouse = plyr:GetMouse()

mouse.KeyDown:connect(function(key)
	if key:byte() == 32 then --32 is space.
		plyr.Character.Humanoid.Jump = true
	end
end)

Fun Facts about KeyDown

  • There is KeyUp which is not as popular as KeyDown.


UserInputService

Use InputBegan to see what keys the player pressed:

--LocalScript
local plyr = game:GetService("Players").LocalPlayer
local uis = game:GetService("UserInputService")

uis.InputBegan:connect(function(key)
	if key.KeyCode == 32 then
		plyr.Character.Humanoid.Jump = true
	end
end)

Fun Facts about UserInputService

  • You can now play roblox with a game console because of user input service!



Final Script

--LocalScript
local plyr = game:GetService("Players").LocalPlayer
local uis = game:GetService("UserInputService")

for _, controller in pairs(game:GetService("ControllerService"):GetChildren()) do
	controller:Destroy()
end

uis.InputBegan:connect(function(key)
	if key.KeyCode == Enum.KeyCode.Space then
		if plyr.Character:FindFirstChild("Humanoid") then
			plyr.Character.Humanoid.Jump = true
		end
	end
end)




Hope this helps!

What You Need To Do

  • Use the InputBegan Event of UserInputService

This Event detects any user input. If you compare the value returned(the user input) with any KeyCode Enum, in your case.. 'Space', then you can activate the Jump Property of the character's Humanoid. this will make them jump


Code

--UserInputService
local uis = game:GetService('UserInputService')
--Player
local plr = game.Players.LocalPlayer

--InputBegan
uis.InputBegan:connect(function(input)
	--Check if the input is space
	if input.KeyCode == Enum.KeyCode.Space then
		--Activate Jump property
		local hum = plr.Character:WaitForChild('Humanoid')
		if hum then
			hum.Jump = true
		end
	end
end)

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