How to prevent the player from moving forward? (so that W key is disabled while other keys are still allowed)

Im trying to prevent the player from getting to close to another player.
This is my code:

if game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.W) and (TargetPos -Char.HumanoidRootPart.Position).Magnitude < Distance then
	Hum.WalkSpeed = 0
else
	Hum.WalkSpeed = Class.ToolVariables.NormalSpeed
end

The problem with this is that when the player for example presses W and D, he is still stuck in one place. Is there a way to just disable and enable W.

I wouldn’t use that code because this will only works for PC

Right now its more like a prototype so i will deal with compatibility later

1 Like
RunService.Heartbeat:Connect(function() 
    if ((Humanoid.MoveDirection).Unit.Z > 0) then
        Humanoid.WalkSpeed = 0
    else
        Humanoid.WalkSpeed = Class.ToolVariables.NormalSpeed
    end
end)
1 Like

Most people are not aware of this, but binding an action through context action service makes the button unusable for any other purpose.

Just this simple code does what you ask for:

local contextActionService = game:GetService("ContextActionService")

contextActionService:BindAction("NoWalk", function() end, false, Enum.KeyCode.W, Enum.KeyCode.Up)
3 Likes
UserInputService.InputBegan...(function(Input, GameProcessedEvent)
   if not GameProcessedEvent and Input.Keycode == ... then
      Hum.WalkSpeed = 0
   else
      Hum.WalkSpeed = ...
   end
end)
local UIS=game:GetService("UserInputService")
local player=game:GetService("Players").LocalPlayer
local character=player.Character or player.CharacterAdded:Wait()
local humanoid=character:WaitForChild("Humanoid")
local walkSpeed=humanoid.WalkSpeed

UIS.InputBegan:Connect(function(input,gameProcessedEvent)
	if gameProcessedEvent then return end
	if input.KeyCode==Enum.KeyCode.W then
		humanoid.WalkSpeed=0
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode==Enum.KeyCode.W then
		humanoid.WalkSpeed=walkSpeed
	end
end)

This is by far the best solution.
Even if not necessary, I also feel like returning Enum.ContextActionResult.Sink is more explicit.

Try my suggestion … works perfectly and you can add whatever key/press you wish.
(Many attempts with many different ways)

Even figured out one I can use with something I’m doing …
Just stops all Control …

Summary
task.wait(3)
local player=game:GetService("Players").LocalPlayer
local playerScripts = player:WaitForChild("PlayerScripts")
local PlayerModule = require(playerScripts:Waitforchild("PlayerModule"))
local controls = PlayerModule:GetControls()

controls:Disable()

Yes, but your solution is not scalable. What if your game allows your character to run by pressing shift? Now you have to add an if condition in the run script to prevent it from working when the character can’t move forward, but oh no! Now you can’t run backwards, even if that should be possible (we only disabled walking forwards, remember?) and so you have to add another if condition…

The script to stop all control is better, but again, not quite right, as it disables all controls instead of disabling just walking forwards.

Yeah thats the best solution by far. It perfectly fits my needs, optimized and easily customizable. Thx.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.