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 9 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 — 9y

2 answers

Log in to vote
2
Answered by 9 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.

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!

Ad
Log in to vote
4
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 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

--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)

Answer this question