Hello,
I’m trying to make a movement that you can only go forward(w). How can I do this?
Thank you in advance.
*Edited added jump command
Create a local script inside of StarterPlayer > StarterPlayerScripts and name the script “ControlScript” then paste the following script
local usin = game:GetService('UserInputService')
local walkControls = {
Enum.KeyCode.W
}
local jumpcontrols = {
Enum.KeyCode.Space
}
local walking = false
usin.InputBegan:connect(function(input)
for i=1,#walkControls do
if input.KeyCode == walkControls[i] then
walking = true
end
end
for i=1,#jumpcontrols do
if input.KeyCode == jumpcontrols[i] then
local char =game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name)
if char then
local humanoid = char:FindFirstChild("Humanoid")
if humanoid then
humanoid.Jump = true
end
end
end
end
end)
usin.InputEnded:connect(function(input)
for i=1,#walkControls do
if input.KeyCode == walkControls[i] then
walking = false
local char =game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name)
if char then
local humanoid = char:FindFirstChild("Humanoid")
if humanoid then
humanoid.WalkToPoint = char:WaitForChild("HumanoidRootPart").Position
end
end
end
end
end)
local debounce = false
game:GetService('RunService').RenderStepped:Connect(function()
wait(.1)
if debounce ==false then
debounce = true
if walking == true then
local char =game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name)
if char then
local humanoid = char:FindFirstChild("Humanoid")
if humanoid then
humanoid.WalkToPoint = char:WaitForChild("HumanoidRootPart").Position+char:WaitForChild("HumanoidRootPart").CFrame.LookVector * 35
end
end
end
debounce = false
end
end)
I don’t mean to be picky, but for me it had some studer and also I tried searching up and can you edit this to work please it seems to be more simpler?
local player = game.Players.LocalPlayer
local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")
local fowardValue = 0
local function onFoward(actionName, inputState)
if inputState == Enum.UserInputState.Begin then
fowardValue = 1
elseif inputState == Enum.UserInputState.End then
fowardValue = 0
end
end
RunService:BindToRenderStep("Control", Enum.RenderPriority.Input.Value)
ContextActionService:BindAction("Foward", onFoward(), true, "w", Enum.KeyCode.W, Enum.KeyCode.DPadUp)
Sorry for being misleading.
wait()
local player = game.Players.LocalPlayer
local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")
local forwardValue = 0
local function onForward(actionName, inputState)
if inputState == Enum.UserInputState.Begin then
forwardValue = 1
elseif inputState == Enum.UserInputState.End then
forwardValue = 0
end
end
local function onJump(actionName, inputState)
if inputState == Enum.UserInputState.Begin then
if player.Character ~= nil then
player.Character:FindFirstChild("Humanoid").Jump = true
end
end
end
local debounce = false
local rs = game:GetService('RunService')
rs.RenderStepped:connect(function()
wait(.01)
if player.Character ~= nil then
debounce = true
local hum = player.Character:FindFirstChild("Humanoid")
if hum then
local root = hum.Parent:FindFirstChild("HumanoidRootPart")
if root then
if forwardValue > 0 then
hum.WalkToPoint = root.Position+root.CFrame.LookVector*200
else
hum.WalkToPoint = root.Position
end
end
end
debounce = false
end
end)
ContextActionService:BindAction("Forward", onForward, true, Enum.KeyCode.W, Enum.KeyCode.DPadUp)
ContextActionService:BindAction("Jump", onJump, true, Enum.KeyCode.Space, Enum.KeyCode.ButtonA)
Copy paste this script from PlayerScripts when you test the game, then place it into StarterPlayerScripts
Then, simply remove these lines from the script PlayerModule.ControlModule.Keyboard
Also, remove the same actions from this function (except for moveForwardAction), otherwise you’ll get an error
(you might want to keep JumpAction)
When you’re done, the code around here should look like this
I only showed how to do this with Keyboard
because I have no idea how to do it with Gamepad/Touch Controls
Since I cannot test this right now I’m just wondering what fires this or is it already in the script?
This is Roblox’s internal movement control script, which you can edit. I’m removing the actions that bind the arrow keys to their respective moment keys (except for up/w)
If you’re feeling a bit extra spicy, you could make it toggleable by modifying the functions to check an attribute without removing the Context bind
local handleMoveBackward = function(actionName, inputState, inputObject)
if not LocalPlayer:GetAttribute("AllowMovementOtherThanForward") then
return Enum.ContextActionResult.Sink
end
self.backwardValue = (inputState == Enum.UserInputState.Begin) and 1 or 0
self:UpdateMovement(inputState)
return Enum.ContextActionResult.Pass
end