In my tool “Wheelbarrow” is a local script which does a animation when the tool gets equipped.
But I didn’t figure out how to make the animation stop when the tool gets unequipped. Can someone help me?
Thats the script:
local Tool = script.Parent
local R15 = script:WaitForChild(“R15”, 1) – Skript finden R15 Animation
local R6 = script:WaitForChild(“R6”, 1) – Skript finden R6 Animation
Tool.Equipped:Connect(function()
local Character = Tool.Parent
if Character then
local Humanoid = Character:FindFirstChildWhichIsA(“Humanoid”)
if Humanoid then
local LoadedAnim = nil
if Humanoid.RigType == Enum.HumanoidRigType.R15 and R15 then
LoadedAnim = Humanoid:LoadAnimation(R15)
elseif Humanoid.RigType == Enum.HumanoidRigType.R6 and R6 then
LoadedAnim = Humanoid:LoadAnimation(R6)
end
if LoadedAnim then
LoadedAnim:Play()
end
end
end
end)
local LoadedAnimation
Tool.Equiped:Connect(function()
--Stuff
end)
Tool.Unequiped:Connect(function()
if LoadedAnimation then
LoadedAnimation:Stop()
LoadedAnimation = nil
end
end)
local Tool = script.Parent
local R15 = script:WaitForChild("R15", 1) -- R15 Animation
local R6 = script:WaitForChild("R6", 1) -- R6 Animation
local LoadedAnim
Tool.Equipped:Connect(function()
local Character = Tool.Parent
if not Character then return end
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
if not Humanoid then return end
if Humanoid.RigType == Enum.HumanoidRigType.R15 and R15 then
LoadedAnim = Humanoid:LoadAnimation(R15)
LoadedAnim:Play()
elseif Humanoid.RigType == Enum.HumanoidRigType.R6 and R6 then
LoadedAnim = Humanoid:LoadAnimation(R6)
LoadedAnim:Play()
end
end)
function StopAnim()
if LoadedAnim then
LoadedAnim:Stop()
end
end
Tool.Unequipped:Connect(StopAnim)
--creating a different thread, so even if the script gets destroyed it will still run
task.spawn(function()
while Tool.Parent ~= nil do
task.wait(.05)
end
StopAnim()
end)
You forgot to complete these if statements with end.
if Character then
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
if Humanoid then
local LoadedAnim = nil
if Humanoid.RigType == Enum.HumanoidRigType.R15 and R15 then
LoadedAnim = Humanoid:LoadAnimation(R15)
elseif Humanoid.RigType == Enum.HumanoidRigType.R6 and R6 then
LoadedAnim = Humanoid:LoadAnimation(R6)
end
end
end
local Player = game.Players.LocalPlayer
local AnimationTrack = script.Animation -- Add animation inside the script.
local Animator = Player.Character:WaitForChild("Humanoid"):FindFirstChildOfClass("Animator")
local Animation = Animator:LoadAnimation(AnimationTrack)
script.Parent.Equipped:Connect(function()
Animation:Play()
end)
script.Parent.Unequipped:Connect(function()
if Animation then
Animation:Stop()
task.wait(0.01)
end
end)
I get this error when touching the part but its still working.
Script:
script.Parent.Touched:Connect(function(hit)
local Sound = game.SoundService.FirstTask
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local lemon = player.Backpack:FindFirstChild(“Wheelbarrow”) or player.Character:FindFirstChild(“Wheelbarrow”) --Replace with your tool name
if lemon then
local char = player.Character
local equippedTool = char and char:FindFirstChildWhichIsA(“Tool”)
if equippedTool then
local ArrowEvent = game.ReplicatedStorage.ArrowEvent
ArrowEvent:FireAllClients(script.Parent, false)
lemon:Destroy()
end
end
end)
The error is gone, but the script still doesn’t work, probably because of the sound. Should I turn it into a LocalScript? Would I need to change anything then?