Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is my script not playing an animation?, im new to lua, and the output doesnt show any problem

Asked by 2 years ago

local tool = script.Parent local anim = tool:WaitForChild("animation") local player = game:GetService("Players").LocalPlayer local char = player.CharacterAdded:Wait() local humanoid = char:WaitForChild("Humanoid") local loadanim = humanoid:LoadAnimation(anim)

tool.Activated:Connect(function() loadanim:Play() end)

I already have the animation on the tool with the id

3 answers

Log in to vote
0
Answered by 2 years ago

First of all, for future questions, can you format your script. that way its easier for people to answer

Second your problem is your waiting for CharacterAdded to run again, which if the player has the tool already, it will run right away, so changing local char = player.CharacterAdded:Wait() to local char = player.Character should be your fix.

Alongside this. Humanoid:LoadAnimation() is no longer as in use, you should instead use Humanoid.Animator:LoadAnimation() as its what is recommended.

0
Oh my bad, how do i format the script? And the script works with only animations that are not the hand of the tool, by this I mean that when it is another hand that isnt the one holding the tool it works if it is another it doesnt work. naninholeo 0 — 2y
Ad
Log in to vote
0
Answered by 2 years ago
Edited 2 years ago
wait()
--// Services:
local players = game:GetService("Players")
--// Variables:
local animation = script:WaitForChild(Animation Name)
local tool = script.Parent
--// Character:
local player = players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local loadedAnimation = animator:LoadAnimation(animation)
--// Events:
tool.Activated:Connect(function()
    loadedAnimation:Play()
end)

This should help

0
Still the same problem, the hand holding the tool doesnt play animations https://prnt.sc/F-ls0rRocSn7 https://prnt.sc/KBPcCMkPEr4H naninholeo 0 — 2y
Log in to vote
0
Answered by 2 years ago

Oh nvm i found the answer. The answer was the priority

wait()
local players = game:GetService("Players")
local animation = script:WaitForChild("Animation")
local tool = script.Parent
local player = players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local loadedAnimation = animator:LoadAnimation(animation)
loadedAnimation.Priority = Enum.AnimationPriority.Action

tool.Activated:Connect(function()
    loadedAnimation:Play()
end)

I added the priority and it works. Thank u both.

Answer this question