Basically I want my customized-animated run animation to sync with the WalkSpeed the player is going, although I don’t know how to entirely script that.
Along with that, I want my walk animation to be minimal of the speed of 1 - 8, therefore then changing to the run-animation where it will accelerate faster depending on the walkspeed the player or NPC is going.
You can override the default animations that are put into the player’s character to do this. In order to get access to the default animation script and animations, run your game and navigate to your player’s character model in the workspace. Copy the animation script, stop the game, and paste it into your StarterCharacterScripts. Placing it there will override the default animation script with that one, including any edits you add to it. In the script you copied over, you can edit all the animation ID’s in certain areas to your own ID’s. These include the running and walking animations.
You can probably create a connection to RunService.Heartbeat that speeds up or slows down your animation track based on Humanoid:GetMoveVelocity().Magnitude.
You could additionally create a connection to disconnect the heartbeat connection when the animation track finishes.
If you replace the walking animation with something different, it should sync to the walkspeed and work just like a regular walking animation. The character code can we weird though, so there might be something off about it.
Here is some code I wrote, I haven’t tested it. The other method might be easier if it’s just for a walking animation.
local RunService = game:GetService("RunService")
local REGULAR_WALK_SPEED = 5 -- Pick a number to make the speed right
local function createConnection(animationTrack, humanoid)
-- Will adjust the animation speed until we stop it
local connection = RunService.Heartbeat:Connect(function()
local humanoidWalkSpeed = humanoid:GetMoveVelocity().Magnitude
local animationSpeed = humanoidWalkSpeed / REGULAR_WALK_SPEED
animationTrack:AdjustSpeed(animationSpeed)
end)
-- Returns a function to stop the adjustments
return function()
connection:Disconnect()
end
end
-- Example usage
local humanoid = ...
local animationTrack = ...
local stopFunction = createConnection(animationTrack, humanoid)
-- Now the animationTrack will be adjusted by the humanoid's move speed until we run stopFunction
-- Say we want the adjustment to stop when the animation track ends
animationTrack.Ended:Once(stopFunction)
-- Stop adjusting if the humanoid is destroyed
humanoid.AncestryChanged:Connect(function(_, newParent)
if newParent == nil then
stopFunction()
end
end)
I’m not sure where that error is, it might be from how the code was used. I’m also not sure if the GetMoveVelocity function is meant to be exposed though (the new docs suck).
Here is some code to run from the server:
local function switchWalk(character, newAnimation)
local walkValue = character:WaitForChild("Animate"):WaitForChild("walk")
local oldWalkAnim = walkValue:FindFirstChildOfClass("Animation")
oldWalkAnim:Destroy()
local newWalkAnim = newAnimation:Clone()
newWalkAnim.Parent = walkValue
end
local function switchRun(character, newAnimation)
local runValue = character:WaitForChild("Animate"):WaitForChild("run")
local oldRunAnim = runValue:FindFirstChildOfClass("Animation")
oldRunAnim:Destroy()
local newRunAnim = newAnimation:Clone()
newRunAnim.Parent = walkValue
end
I had a typo (I had the run animation be parented to walkValue but it should have been runValue), maybe try it now. Set your run animation to both the walk and run animation. newAnimation should be an Animation. It has to be done from the server too.
local function switchWalk(character, newAnimation)
local walkValue = character:WaitForChild("Animate"):WaitForChild("walk")
local oldWalkAnim = walkValue:FindFirstChildOfClass("Animation")
oldWalkAnim:Destroy()
local newWalkAnim = newAnimation:Clone()
newWalkAnim.Parent = walkValue
end
local function switchRun(character, newAnimation)
local runValue = character:WaitForChild("Animate"):WaitForChild("run")
local oldRunAnim = runValue:FindFirstChildOfClass("Animation")
oldRunAnim:Destroy()
local newRunAnim = newAnimation:Clone()
newRunAnim.Parent = runValue
end
I tried it in studio with this code and it worked:
local function switchWalk(character, newAnimation)
local walkValue = character:WaitForChild("Animate"):WaitForChild("walk")
local oldWalkAnim = walkValue:FindFirstChildOfClass("Animation")
oldWalkAnim:Destroy()
local newWalkAnim = newAnimation:Clone()
newWalkAnim.Parent = walkValue
end
local function switchRun(character, newAnimation)
local runValue = character:WaitForChild("Animate"):WaitForChild("run")
local oldRunAnim = runValue:FindFirstChildOfClass("Animation")
oldRunAnim:Destroy()
local newRunAnim = newAnimation:Clone()
newRunAnim.Parent = runValue
end
game:GetService("Players").PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
-- Set to some animation
local animation = character:WaitForChild("Animate"):WaitForChild("dance"):FindFirstChildOfClass("Animation")
switchWalk(character, animation)
switchRun(character, animation)
end)
end)