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?
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.
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.
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
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)