[solved]
Problem was that the ChildAdded function triggered too. For some reason the 2nd animation fired after i update my character model (By un-equipping the tool).
Fixed this by adding
if #script.Parent:GetChildren() == #game.StarterPack:GetChildren() then
[--------]
The code below runs an animation, equal to the name of the tool, on equiping. For some reason, it also runs the animation on unequipping, even though i never call PlayAnimation() on Unequipped. Why could this happen?
If more detail is required, feel free to comment below.
local Animations = {
["Minigun"] = 589289270,
["RocketMinigun"] = 589289270 ,
["AK47"] = 0,--todo
["Mosin nagant"] = 0--todo
}
function PlayAnim(ToolName)
local a = Instance.new("Animation")
a.AnimationId = "rbxassetid://"..Animations[ToolName]
local h = game.Players.LocalPlayer.Character.Humanoid
local t = h:LoadAnimation(a)
t:Play()
a:Destroy()
end
function CreateToolLibrary(Tool)
Tool.Equipped:Connect(function()
PlayAnim(Tool.Name)
end)
end
wait(0.1)
script.Parent.ChildAdded:Connect(function(Child)
if Child:IsA("Tool") then
CreateToolLibrary(Child)
end
end)
for _,v in pairs(script.Parent:GetChildren()) do
if v:IsA("Tool") then
CreateToolLibrary(v)
end
end
