Hi, I’m experiencing a few issues with sword animations which are mentioned below:
Despite preloading animations, the sword animation doesn’t play till after the player moves then stops. The animation priority is set to ‘Idle’.
The idle animation doesn’t stop playing after the sword is unequipped till the player moves then stops.
LocalScript:
-- Locations
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Sword : Tool = script.Parent
-- Player
local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local AnimateScript = Character.Animate
local Equipped = false
-- Animations
local ContentProvider = game:GetService("ContentProvider")
local Module = require(ReplicatedStorage:FindFirstChild("RS_Abilities").MainModule)
ContentProvider:PreloadAsync(script:GetChildren())
local SwordIdle = script.SwordIdle.AnimationId
local SwordWalk = script.SwordWalk.AnimationId
-- Sword Toggle
Sword.Equipped:Connect(function()
AnimateScript.idle.Animation1.AnimationId = SwordIdle
Equipped = true
end)
Sword.Unequipped:Connect(function()
AnimateScript.idle.Animation1.AnimationId = Module.Default.IdleAnimation1
AnimateScript.walk.WalkAnim.AnimationId = Module.Default.WalkAnimation
Equipped = false
end)
-- Walk Animations
UserInputService.InputBegan:Connect(function(key, gameProcessedEvent)
if gameProcessedEvent then return end
if Equipped then
AnimateScript.walk.WalkAnim.AnimationId = SwordWalk
end
end)
UserInputService.InputEnded:Connect(function(key, gameProcessedEvent)
if gameProcessedEvent then return end
if Equipped then
AnimateScript.idle.Animation1.AnimationId = SwordIdle
end
end)
Hey, thanks for your solution. I was wondering how to get the walk animation to play properly when the LocalScript detects MoveDirection.
Humanoid.StateChanged:Connect(function()
if Equipped and Humanoid.MoveDirection.Magnitude > 0 then
SwordWalkTrack:Play()
elseif Equipped and Humanoid.MoveDirection.Magnitude <= 0 then
SwordWalkTrack:Stop()
end
end)
As for me I prefer to put it into a function. You don’t have to do the same.
But instead of elseif it’s better to do else then stop animations. And also if the player moves and the animation doesn’t play and it checks that it’s not playing it will play else it stops the animation
You’ll also need to add
RunService.Heartbeat, it allows you to check the player’s movements.
Examples of how I would do it.
local function checkMovement()
while true do
RunService.Heartbeat:Wait()
if Equipped then
if Humanoid.MoveDirection.Magnitude > 0 then
if not SwordWalkAnim.IsPlaying then
SwordWalkAnim:Play()
end
else
if SwordWalkAnim.IsPlaying then
SwordWalkAnim:Stop()
end
end
end
end
end
checkMovement()