I want an animation to play when I pick up the tool. These are my codes:
local tool = script.Parent
local anim = tool.Animation2
local oyuncu = game:GetService("Players").LocalPlayer
local char = oyuncu.Character
local humanoid = char:WaitForChild("Humanoid")
local loadanim = humanoid:LoadAnimation(anim)
function aldi()
loadanim:Play()
end
tool.Equipped:connect(aldi)
But my codes are not working. I want help with this.
Okay so, maybe the oyuncu.Character is coming back as nil? so maybe try waiting for the Character to spawn. (this may not be the issue since i know it might’ve errored)
local char = Player.Character or Player.CharacterAdded:Wait()
if Player.Character is nil it will wait for the Player.CharacterAdded event to fire.
also make sure the Animation AnimationPriority is set to action or something that can override the player’s character current playing animations (like walking, idle and etc). you change it in the animation editor. is this your animation? if not, try testing it on roblox not on roblox studio play testing.
First of all, the connect function is deprecated. Capitalize the first letter.
tool.Equipped:Connect(aldi)
Second, the LoadAnimation function, which is connected to the humanoid of the character, is deprecated, as the others mentioned. Use it with the Animator.
And make sure the RequiresHandle property of the tool is set to false.
Finally, as @mike00401 suggested, define the character as he said. If the first one had not found the character, the second would have been used. And if not second, the first one. This is a better way to get it without any errors.
local tool = script.Parent
local anim = tool.Animation2
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local loadanim = animator:LoadAnimation(anim)
tool.Equipped:Connect(function()
loadanim:Play()
end)
--Player Variables
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
--Tool & Animation
local tool = script.Parent
local animation = tool:WaitForChild("Equip1") --Change this to your animation name
local Humanoid = Character:WaitForChild("Humanoid")
local equipAnim = Humanoid:LoadAnimation(animation)
tool.Equipped:Connect(function()
equipAnim:Play()
end)
tool.Unequipped:Connect(function()
equipAnim:Stop()
end)
Are you sure you have the correct animation ID? Let us know the animation where it comes from. What is the animation ID?
And also add print wherever you want so you can find the error where it occurs.
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
print("Character is found")
--Tool & Animation
local tool = script.Parent
local animation = tool:WaitForChild("Animation2")
print("Animation is found")
local Humanoid = Character:WaitForChild("Humanoid")
local equipAnim = Humanoid:LoadAnimation(animation)
print("Animation is loaded")
tool.Equipped:Connect(function()
print("Equipped")
equipAnim:Play()
end)
tool.Unequipped:Connect(function()
print("Unequipped")
equipAnim:Stop()
end)
What priority is your animation in?
it won’t work if it not in “Action” priority. You can change it by:
If you haven’t deleted the dummy you made animation in, you can animate it again and the previous animation will show and just change priority and export it again
If not remake animation with Action priority
Have a nice day!