Tool freezes client when equipped and unequipped

Hello. I have a tool here that plays an animation for drinking. It destroys the AnimationTrack when you unequip it, and you have to wait for the animation to finish to play it again. However, the problem is, it lags/freezes for me and makes the game unplayable for the player when you equip and unequip the tool. Here is my script, all help is appreciated.

	local Tool = script.Parent

	local playing = false		

Tool.Equipped:Connect(function(Mouse)
	local Character = Tool.Parent --players character model
	local AnimationTrack = Character.Humanoid:LoadAnimation(script.Parent.EatAnimation)
	Tool.Activated:Connect(function()--when a player clicks while they are holding the tool
			
     
    

		
		if playing == false then
			
			wait()
			AnimationTrack:Play()
			
			wait()
			playing = true--makes playing true 
			
			wait()
			AnimationTrack.Stopped:Wait()--wauts until the animation is finished to play it again
			playing = false--makes playing false again
			AnimationTrack:Destroy()--destroys it since it creates a new animationtrack every time the tool is equipped
		
			
		end
			Tool.Unequipped:Connect(function()
		
			wait()
		AnimationTrack:Destroy()--destroys it when the tool is unequipped since it creates a new animationtrack every time the tool is equipped
	end)
		
	
		
end)

	


end)

Why are you reloading the Animation every single time you equip the Tool? That is extremely inefficient, and means that there will be load time delay before the Animation can run, every time you equip the Tool.

Wouldn’t it be better to load it once at the start of your script, and just use AnimationTrack:Stop() to halt the animation , instead of deleting it?

1 Like

Yes, but I can only get the players Character when they equip the tool.

Use

local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:wait()

It’s easier to use.

I would, but it’s a regular script. I will try it with a localscript tho

Also, you don’t have to put the Unequip and Activated functions both under the Equip function. Just have them separate.

Animations only load on a local script.

Why can’t you detect the Player from the Tool’s heritage at the start of the script? If the Tool is in the Player’s backpack, you can get the Player easily since the Backpack is a child of the Player.

For example:

Player = Tool.Parent.Parent

Also, if shifting your code over to a LocalScript is an option, that would be a good solution.

Well, they loaded for me with a Script

Animations load fine from ServerScript’s. The only reason you would use a LocalScript, is to remove the slight replication time of the animation for the local player.

I tried checking the Backpack’s parent, but that returned nil

Can you show me the code you used? Is the Tool in the Player’s Backpack at the time the code runs?

local Tool = script.Parent

local playing = false		
local Character = script.Parent.Parent.Parent:WaitForChild('Character')
local AnimationTrack = Character:WaitForChild('Humanoid'):LoadAnimation(Tool.EatAnimation)
Tool.Equipped:Connect(function(Mouse)
	
	
	Tool.Activated:Connect(function()
			
     
    

		
		if playing == false then
			
			wait()
			AnimationTrack:Play()
			
			wait()
			playing = true--makes playing true 
			
			wait()
			AnimationTrack.Stopped:Wait()--wauts until the animation is finished to play it again
			playing = false--makes playing false again
			AnimationTrack:Destroy()--destroys it since it creates a new animationtrack every time the tool is equipped
		
			
		end
			Tool.Unequipped:Connect(function()
		
			wait()
		AnimationTrack:Destroy()--destroys it when the tool is unequipped since it creates a new animationtrack every time the tool is equipped
	end)
		
	
		
end)

	


end)

it returned nil when i didn’t waitforchild(‘humanoid’), but when i did waitforchild for the humanoid, it waited forever

I don’t think your referencing of the Character is quite right.

You would get the Character like so:

local Character = Tool.Parent.Parent.Character

When I do that I get the error: 01:00:45.497 - LoadAnimation requires the Humanoid object (Leninent.Humanoid) to be a descendant of the game object

That just means that the code is running before the Character has been loaded fully.

Doing this should fix the error:

local Character = Tool.Parent.Parent.Character
while Character.Parent == nil do
	Character.AncestryChanged:wait()
end

Thank you, that worked, animation plays now. Now that brings us back to the issue; the tool still freezes when equipped and unequipped.

Could we see your current code, with the changes? It would also be helpful to have a video of the issue, so that we can see what is going on.

Also, I would recommend switching to doing this whole thing on the client as @IGOTHISLOL said. This is the proper way to do it, and might fix your issue.

Here is a video:
robloxapp-20200827-0109100.wmv (708.5 KB)
Here is the script, same thing happens with both on a localscript and a regular script:

local Tool = script.Parent

local playing = false		
local Character = Tool.Parent.Parent.Character or Tool.Parent.Parent.CharacterAdded:Wait()
while Character.Parent == nil do
	Character.AncestryChanged:wait()
end
local AnimationTrack = Character:WaitForChild('Humanoid'):LoadAnimation(Tool.EatAnimation)
Tool.Equipped:Connect(function(Mouse)
	
	
	Tool.Activated:Connect(function()
			
     
    

		
		if playing == false then
			
			wait()
			AnimationTrack:Play()
			
			wait()
			playing = true--makes playing true 
			
			wait()
			AnimationTrack.Stopped:Wait()--wauts until the animation is finished to play it again
			playing = false--makes playing false again
			AnimationTrack:Destroy()--destroys it since it creates a new animationtrack every time the tool is equipped
		
			
		end
			Tool.Unequipped:Connect(function()
		
			wait()
		AnimationTrack:Destroy()--destroys it when the tool is unequipped since it creates a new animationtrack every time the tool is equipped
	end)
		
	
		
end)

	


end)

It looks fine! Are there any problems now?