How do I detect if a player is moving backwards?

I wanna know how I can detect if a player is moving backwards.

Thanks.

1 Like

I am pretty sure you could do something like this in a local script:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChild("Humanoid")

if Humanoid.MoveDirection.Magnitude < 0 then
    print("Hi")
end

Correct me if I am wrong, but I think that will work.

Hope this helps :slightly_smiling_face:

Doesn’t work sorry. [ Blank space ]

Try this:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChild("HumanoidRootPart")

if Humanoid.MoveDirection.Magnitude =< 0 then
    print("Hi")
end
1 Like

Also doesn’t work sorry. [ Filler ]

Wrap it in a loop. That only checks once when your character is added.

I did, and I tried to print it and when you are just moving backwards it just stays at zero. And the game is currently in first person so yeah, it doesn’t work.

Use UserInputService to check if the player is holding the ‘S’ key.

local UserInputService = game:GetService("UserInputService")

local isHolding = false

UserInputService.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.S then
		isHolding = true
		
		while isHolding do
			print("Holding S key")
			wait(0.3)
		end
	end
end)

UserInputService.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.S then
		isHolding = false
	end
end)

I also need this script to be compatible with mobile so I don’t really know how to do that.

This prints every 0.3 seconds the player is walking backwards.

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")

while wait(0.3) do
	if humanoid.MoveDirection.Z > 0 then
		print("Player is moving backwards")
	end	
end

Alright I’ll try it. [ Filler ]

It doesn’t work sorry. When you walk backwards on the X-axis it doesn’t seem to work.

It works, I know that. If not something is wrong with your script or game.

It’s probably because my game is in first person.

Have you copied my script and tried it? It should not have anything to do with it being in first person. If you walk backwards with the ‘S’ key it prints.

But the thing is this game needs to be compatible with mobile. So I don’t really know how to do that.

It is compatible with mobile, the script

Yep [ Filler again cause it’s way too short ]

But I am trying to figure out a way using the HumanoidRootPart’s CFrame.LookVector:Dot feature.

There are a bunch of topics made with this exact title. Use the search bar.