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

Disabling WASD/Arrow Key Movements?

Asked by 10 years ago

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?

0
Please look at both answers! Both Goulstem and I worked hard to make these answers! EzraNehemiah_TF2 3552 — 10y

2 answers

Log in to vote
2
Answered by 10 years ago

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.

1workspace.Player.Humanoid.Jump = true

Fun Facts about Jump property

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


KeyDown

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

1--LocalScript
2local plyr = game:GetService("Players").LocalPlayer
3local mouse = plyr:GetMouse()
4 
5mouse.KeyDown:connect(function(key)
6    if key:byte() == 32 then --32 is space.
7        plyr.Character.Humanoid.Jump = true
8    end
9end)

Fun Facts about KeyDown

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


UserInputService

Use InputBegan to see what keys the player pressed:

1--LocalScript
2local plyr = game:GetService("Players").LocalPlayer
3local uis = game:GetService("UserInputService")
4 
5uis.InputBegan:connect(function(key)
6    if key.KeyCode == 32 then
7        plyr.Character.Humanoid.Jump = true
8    end
9end)

Fun Facts about UserInputService

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



Final Script

01--LocalScript
02local plyr = game:GetService("Players").LocalPlayer
03local uis = game:GetService("UserInputService")
04 
05for _, controller in pairs(game:GetService("ControllerService"):GetChildren()) do
06    controller:Destroy()
07end
08 
09uis.InputBegan:connect(function(key)
10    if key.KeyCode == Enum.KeyCode.Space then
11        if plyr.Character:FindFirstChild("Humanoid") then
12            plyr.Character.Humanoid.Jump = true
13        end
14    end
15end)




Hope this helps!

Ad
Log in to vote
4
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
10 years ago

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

01--UserInputService
02local uis = game:GetService('UserInputService')
03--Player
04local plr = game.Players.LocalPlayer
05 
06--InputBegan
07uis.InputBegan:connect(function(input)
08    --Check if the input is space
09    if input.KeyCode == Enum.KeyCode.Space then
10        --Activate Jump property
11        local hum = plr.Character:WaitForChild('Humanoid')
12        if hum then
13            hum.Jump = true
14        end
15    end
16end)

Answer this question