Script works first time, than breaks the next?

Basically I am making a tool with animations, but I am having trouble loading the animations, getting the humanoid etc.

local blade = script.Parent.BladeGlow1
local LightSaber = script.Parent

script.Parent.Equipped:Connect(function()
	local Character = LightSaber.Parent
	local humanoid = Character.Humanoid -- Works here perfectly fine

	local Idle = humanoid:LoadAnimation(script:WaitForChild('Idle'))
	local EquipAnim = humanoid:LoadAnimation(script:WaitForChild('Equip'))

	EquipAnim.Priority = Enum.AnimationPriority.Movement
	EquipAnim:Play()
	wait(1)
	Idle.Looped = true
	Idle.Priority = Enum.AnimationPriority.Movement
	Idle:Play()
end)

script.Parent.Unequipped:Connect(function()
	local Character = LightSaber.Parent
	local humanoid = Character.Humanoid -- doesn't work here for some reason????

	local Idle = humanoid:LoadAnimation(script:WaitForChild('Idle'))
	
	Idle.Looped = false
end)

For some reason the humanoid variable only works when I put it in the equipped function.

Doesn’t work in thee unequipped function or outside of the functions.

Humanoid is not a valid member of Backpack “Players.Rvzol.Backpack”

Load the animations at the beginning of your script, not every time the tool is enabled.

This line:

	local Character = LightSaber.Parent

The LightSaber is a member of the BackPack, not the Humanoid.

2 Likes

When the gear is unequipped, its parent is replaced to the backpack of the player, so its not possible to initialize the Character from .Parent since its new parent is .Backpack

local blade = script.Parent.BladeGlow1
local LightSaber = script.Parent


local Character

script.Parent.Equipped:Connect(function()
	Character = LightSaber.Parent
	local humanoid = Character.Humanoid -- Works here perfectly fine

	local Idle = humanoid:LoadAnimation(script:WaitForChild('Idle'))
	local EquipAnim = humanoid:LoadAnimation(script:WaitForChild('Equip'))

	EquipAnim.Priority = Enum.AnimationPriority.Movement
	EquipAnim:Play()
	wait(1)
	Idle.Looped = true
	Idle.Priority = Enum.AnimationPriority.Movement
	Idle:Play()
end)

script.Parent.Unequipped:Connect(function()
	local humanoid = Character.Humanoid -- doesn't work here for some reason????

	local Idle = humanoid:LoadAnimation(script:WaitForChild('Idle'))

	Idle.Looped = false
end)

This should work.

I hope this help you and if you have questions please reply

2 Likes

This gets rid of the error message but for some reason when I do Idle:Stop() it doesn’t work.
Here is the script



local blade = script.Parent.BladeGlow1
local LightSaber = script.Parent


local Character

script.Parent.Equipped:Connect(function()

	blade.White.Transparency = 1
	blade.Glow.Enabled = false
	blade.Glow1.Enabled = false
	blade.PointLight.Enabled = false
	
	Character = LightSaber.Parent
	local humanoid = Character.Humanoid

	local Idle = humanoid:LoadAnimation(script:WaitForChild('Idle'))
	local EquipAnim = humanoid:LoadAnimation(script:WaitForChild('Equip'))

	EquipAnim.Priority = Enum.AnimationPriority.Movement
	EquipAnim:Play()
	wait(1)
	script.TurnOn:Play()

	blade.White.Transparency = 0
	blade.Glow.Enabled = true
	blade.Glow1.Enabled = true
	blade.PointLight.Enabled = true

	Idle.Looped = true
	Idle.Priority = Enum.AnimationPriority.Movement
	Idle:Play()
end)

script.Parent.Unequipped:Connect(function()
	local humanoid = Character.Humanoid 

	local Idle = humanoid:LoadAnimation(script:WaitForChild('Idle'))
	Idle.Priority = Enum.AnimationPriority.Idle
	Idle:Stop() -- The Animation keeps playing after tool is unequipped
end)

Also is their a way to get around redefining humanoid every time?

nevermind, fixed it with a debounce. haven’t been scripting for a while.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.