This small code snippet lets you know when a player is walking downhill. It works by watching the player’s position and checking if they are moving down without jumping.
To use this code:
- Place in a local script in Game > StarterPlayer > StarterPlayerScripts
- Paste code provided.
Code:
-- Define the local player and the character they control
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
-- Store the previous position of the character's HumanoidRootPart
local prevPosition = character.HumanoidRootPart.Position
-- Function to check if the character is moving downhill
local function checkDownhill()
-- Get the current position of the character's HumanoidRootPart
local currPosition = character.HumanoidRootPart.Position
-- If the character's Y position is lower than the previous position
-- and the character's vertical movement direction is zero,
-- then the character is walking downhill
if currPosition.Y < prevPosition.Y and humanoid.MoveDirection.Y == 0 then
print("Walking downhill") -- Print a message to the console
end
-- Update the previous position with the current position
prevPosition = currPosition
end
-- Connect the checkDownhill function to the Heartbeat event of the RunService,
-- so it's checked every frame
game:GetService("RunService").Heartbeat:Connect(checkDownhill)