I have a sprint script and it has some issues which I think could be fixed if someone could help me. I have a lot of trouble with the sprint animation playing when the character is in the air which you can see I tried to fix but I’m still having trouble. I would like someone to modify the script to only play the sprinting animation if the character is on the ground. If you know how to do this please help or if you can figure out another solution. Thank you!
Here is the script:
-- Services
local userInputService = game:GetService("UserInputService")
local players = game:GetService("Players")
-- Variables
local player = players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://18567622681"
local track = animator:LoadAnimation(animation)
track.Priority = Enum.AnimationPriority.Movement
local RUN_KEYCODE = Enum.KeyCode.LeftControl
local RUN_SPEED = 40
local WALK_SPEED = 16
-- Togglables
local isSprinting = false -- if player has sprinting toggled
local isMoving = false -- if player is moving
local isInAir = false -- if player is in the air (because of jumping)
local function updateAnimation() -- Updates it based on if the player is moving or if they have sprinting on/off
if isSprinting and isMoving then
hum.WalkSpeed = RUN_SPEED
if not track.IsPlaying then -- makes sure it isnt already playing
track:Play()
end
else
hum.WalkSpeed = WALK_SPEED
track:Stop()
end
end
local function inputBegan(input, processed)
if processed then return end
if input.KeyCode == RUN_KEYCODE then
isSprinting = not isSprinting -- toggles on and off
updateAnimation()
end
end
local function moveDirectionChanged()
isMoving = hum.MoveDirection.Magnitude > 0 -- sets var to true/false depending if they are moving or not
if not isInAir then
updateAnimation()
end
end
userInputService.InputBegan:Connect(inputBegan) -- player presses key (we want them to press shift)
hum:GetPropertyChangedSignal("MoveDirection"):Connect(moveDirectionChanged) -- player moves
-- Additional: Stop sprinting if jumping | Redone
userInputService.InputBegan:Connect(function(input, gameEvent)
if not gameEvent then
if input.KeyCode == Enum.KeyCode.Space then
track:Stop()
isInAir = true
end
end
end)
userInputService.InputEnded:Connect(function(input, gameEvent)
if not gameEvent then
if input.KeyCode == Enum.KeyCode.Space then
task.wait(0.4)
isInAir = false
end
end
end)
Try using Humanoid.FloorMaterial to detect whether the player is in the air or not.
Humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
if Humanoid.FloorMaterial == Enum.Material.Air then
isInAir = true
track:Stop()
else
isInAir = false
if isSprinting and isMoving and not track.IsPlaying then
track:Play()
end
end
end)
Sure thing! Tell me if you encounter any issues or errors, because I’ve tested and confirmed that FloorMaterial is a proper property of any Humanoid instance.
local userInputService = game:GetService("UserInputService")
local players = game:GetService("Players")
local player = players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://18567622681"
local track = animator:LoadAnimation(animation)
track.Priority = Enum.AnimationPriority.Movement
local RUN_KEYCODE = Enum.KeyCode.LeftControl
local RUN_SPEED = 40
local WALK_SPEED = 16
local isSprinting = false
local isMoving = false
local isInAir = false
local function floorMaterialChanged()
if hum.FloorMaterial == Enum.Material.Air then
isInAir = true
track:Stop()
else
isInAir = false
if isSprinting and isMoving and not track.IsPlaying then
track:Play()
end
end
end
local function updateAnimation()
if isSprinting and isMoving then
hum.WalkSpeed = RUN_SPEED
if not track.IsPlaying then
track:Play()
end
else
hum.WalkSpeed = WALK_SPEED
track:Stop()
end
end
local function inputBegan(input, processed)
if processed then return end
if input.KeyCode == RUN_KEYCODE then
isSprinting = not isSprinting
updateAnimation()
end
end
local function moveDirectionChanged()
isMoving = hum.MoveDirection.Magnitude > 0
if not isInAir then
updateAnimation()
end
end
userInputService.InputBegan:Connect(inputBegan)
hum:GetPropertyChangedSignal("MoveDirection"):Connect(moveDirectionChanged)
hum:GetPropertyChangedSignal("FloorMaterial"):Connect(floorMaterialChanged)
(An explanation on why you can’t find that property name, it’s because hum isn’t known by the script to be a Humanoid, so it wouldn’t show up, as FloorMaterial is exclusive to Humanoids (I think).)