Hello. I’m currently working on a tool the plays an animation on equipping. While the code runs as is, I’m getting this error:
The animation is an actual instance under the LocalScript of the tool. Wondering what is possibly causing the issue. Here is the code:
-- Get services
local Players = game:GetService("Players")
-- Get player
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local characterValue = player:WaitForChild("Character#")
-- Get tool
local glock = script.Parent
-- Get animations
local idleAnimation = script:FindFirstChild("Idle")
local idleAnimationTrack = nil
character.ChildAdded:Connect(function(child)
if child:IsA("Tool") and child.Name == "Glock" then
if not character then
return
end
local humanoid = character:FindFirstChild("Humanoid")
if not humanoid then
return
end
local animator = humanoid:FindFirstChild("Animator")
if not animator then
return
end
idleAnimationTrack = animator:LoadAnimation(idleAnimation)
idleAnimationTrack:Play()
end
end)
character.ChildRemoved:Connect(function(child)
if child:IsA("Tool") and child.Name == "Glock" then
if idleAnimationTrack then
idleAnimationTrack:Stop()
end
end
end)
maybe the variable for “idleAnimation” is = nil. this is because you either spelt the animation name wrong, or there is another instance with the name “Idle” inside the script.
local idleAnimation = script:FindFirstChild("Idle") -- check name and check instance
Try using script:WaitForChild("Idle"). It might be the animation hasn’t loaded yet, especially considering it runs as one of the first few lines of code in the script.
Are you referring to idleAnimationTrack = nil? Because that becomes defined when the player uses the tool. Also I checked for any spelling errors and any additional instances with that name. I didn’t see anything.
-- Get services
local Players = game:GetService("Players")
-- Get player
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local characterValue = player:WaitForChild("Character#")
-- Get tool
local glock = script.Parent
-- Get animations
local idleAnimation = script:WaitForChild("Idle")
local idleAnimationTrack = nil
character.ChildAdded:Connect(function(child)
if child:IsA("Tool") and child.Name == "Glock" then
if not character then
return
end
idleAnimationTrack = animator:LoadAnimation(idleAnimation)
idleAnimationTrack:Play()
end
end)
character.ChildRemoved:Connect(function(child)
if child:IsA("Tool") and child.Name == "Glock" then
if idleAnimationTrack then
idleAnimationTrack:Stop()
idleAnimationTrack = nil
end
end
end)
try this? maybe it will work, you just gotta get the Humanoid at the first place. (edited)
This didn’t work. The output tells me there is an infinite yield for script:WaitForChild("Idle") unfortunately. I switched it back to FindFirstChild() without any luck either.