i have a gun animation which keep playing until the tool is unequipped, but when I unequip it, it says there is no humanoid because script.parent.parent don’t apply when I unequip the tool, here’s the script:
function OnEquipped()
MyPlayer = PlayersService:GetPlayerFromCharacter(Tool.Parent)
local Players = game:GetService("Players")
--local player = Players.LocalPlayer
local character = script.Parent.Parent
local humanoid = character:WaitForChild("Humanoid")
local takePinOffAnim = script.Animation
local lol = humanoid:LoadAnimation(takePinOffAnim)
lol:Play()
end
function OnUnequipped()
--MyPlayer = PlayersService:GetPlayerFromCharacter(Tool.Parent)
local Players = game:GetService("Players")
--local player = Players.LocalPlayer
local character = script.Parent.Parent
local humanoid = character:WaitForChild("Humanoid")
local takePinOffAnim = script.Animation
local lol = humanoid:LoadAnimation(takePinOffAnim)
lol:Stop()
end
i advise you to set the variables to be a global one, so it would be like this
local character = Tool.Parent
local humanoid = character:WaitForChild("Humanoid")
local takePinOffAnim = script.Animation
local lol = humanoid:LoadAnimation(takePinOffAnim)
function OnEquipped()
lol:Play()
end
function OnUnequipped()
lol:Stop()
end
local GRAVITY_ACCELERATION = workspace.Gravity
local ammo = script.Parent.Ammo
local maxammo = script.Parent.MaxAmmo
local RELOAD_TIME = 0
local ROCKET_SPEED = 1
a = true
local MISSILE_MESH_SCALE = Vector3.new(0.3,0.3,0.3)
local ROCKET_PART_SIZE = Vector3.new(0.1,0.1,1)
local DebrisService = game:GetService('Debris')
local PlayersService = game:GetService("Players")
local MyPlayer
local Tool = script.Parent
local ToolHandle = Tool:WaitForChild('Handle')
local character = Tool.Parent
local humanoid = character:WaitForChild("Humanoid")
local takePinOffAnim = script.Animation
local lol = humanoid:LoadAnimation(takePinOffAnim)
Ah, the blue line wasn’t the error, its the “Infinite yield possible”
anyways i believe this can do it correctly
local Anim
local takePinOffAnim = script.Animation
function OnEquipped()
local character = Tool.Parent
local humanoid = character:WaitForChild("Humanoid")
Anim = humanoid:LoadAnimation(takePinOffAnim)
Anim:Play()
end
function OnUnequipped()
if Anim Then -- just here to be safe
Anim:Stop()
end
end
edit: forgot that tool variable exists on his code
local tool = script.Parent
local animation = nil
function OnEquip()
local plr = game.Players:GetPlayerFromCharacter(tool.Parent)
local character = plr.Character
animation = character:WaitForChild("Humanoid").Animator:LoadAnimation(script:WaitForChild("Animation")
animation:Play()
end
function OnUnequip()
if animation ~= nil then
animation:Stop()
end
end
tool.Equipped:Connect(OnEquip)
tool.Unequipped:Connect(OnUnequip)