I have a tool that the player will use once I give them it from ServerStorage. It works as intended perfectly when in the StarterPack. But as soon as I clone it from ServerStorage the animation doesn’t work at all.
Here’s the script in the tool:
local tool = script.Parent
local plr = game:GetService("Players").LocalPlayer
local char = plr.CharacterAdded:Wait()
local h = char:WaitForChild("Humanoid")
local spawnevent = game.ReplicatedStorage.Tools.PlaceCrate
local objectname = "MedicalCrate"
local Mouse = plr:GetMouse()
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://7076048298"
track = h:LoadAnimation(anim)
track.Priority = Enum.AnimationPriority.Action
track.Looped = false
tool.Equipped:Connect(function()
track:Play()
Mouse.Button1Down:Connect(function()
if Mouse.Target then
if Mouse.Target.Name == "Terrain" then
if plr:DistanceFromCharacter(Mouse.Hit.p) <= 10 and h.Health > 0 then
script.parent.PlaceCrate:Play()
spawnevent:FireServer(objectname,Mouse.Hit)
track:Stop()
tool:Destroy()
end
end
end
end)
end)
tool.Unequipped:Connect(function()
track:Stop()
end)
No! I found the issue, I’m going to help you with this right now, just editing this post right now.
You’re creating the animation outside the equipped function. You’re essentially loading the characters animation but where are you even loading it? To help mitigate this issue, I highly recommend you make it so you’re loading your animation inside equipped. I’ve had an issue like this before, but you essentially write something like this
tool.Equipped:Connect(function()
local Animation = script.Parent.Parent.Humanoid:WaitForChild("Animator"):LoadAnimation(InsertThePathOrVariableToYourAnimationHere)
Animation:Play()
end)
Also couldn’t help but notice that you created an animation, never set it’s parent.
Try putting this:
local anim = Instance.new("Animation", tool) -- replace "tool" with where ever you want the animation to go
Oh, I’ve never known about this before! Yeah don’t use that.
@ScottishKiltBearer I’m sort of confused why Instance.new was needed anyways?
In my eyes it’s far more practical to just create the animation object yourself, reference it in a variable, then load it on the equipped function. Let me get you one of my example scripts.
-- Get the animation folder --
local AnimationFolder = script.Parent.Animations
local ArmRaiseSource = AnimationFolder.ArmRaise
-- Call the Debounce --
local db = false
script.Parent.Equipped:Connect(function()
ArmRaiseAnimation = script.Parent.Parent.Humanoid:WaitForChild("Animator"):LoadAnimation(ArmRaiseSource)
end)
Reason why I never put ArmRaiseAnimation:Play() was because I loaded on equip, then used it inside an activated function, but that didn’t matter for this example.
Also sorry if it sounded like I attacked you for using instance.new
Yeah, I just found the problem. While running the game, I noticed there was a problem creating the animation. It wasn’t even there. So I created an animation in the explorer and done it that way. Also swapped some things around like you said. The final code is this:
local tool = script.Parent
local plr = game:GetService("Players").LocalPlayer
local char = plr.Character
local h = char:WaitForChild("Humanoid")
local spawnevent = game.ReplicatedStorage.Tools.PlaceCrate
local objectname = "MedicalCrate"
local Mouse = plr:GetMouse()
tool.Equipped:Connect(function()
local anim = script.parent.Hold
track = h.Animator:LoadAnimation(anim)
track:Play()
Mouse.Button1Down:Connect(function()
if Mouse.Target.Name == "Terrain" then
if plr:DistanceFromCharacter(Mouse.Hit.p) <= 10 and h.Health > 0 then
script.parent.PlaceCrate:Play()
spawnevent:FireServer(objectname,Mouse.Hit)
track:Stop()
tool:Destroy()
end
end
end)
end)
tool.Unequipped:Connect(function()
track:Stop()
end)