(Note, This is not my “Tool lags when activated” post.)
I have a tool (sword) that lags the entire game when equipping it. I suspect its a “too much instance creation problem” but I don’t know what is causing it.
I have tried destroying stuff that I don’t need in the script (eg destroying useless loaded animations) but didn’t work.
I had a steady 60 fps before equipping, but after, it goes down to 30.
Code:
local Tool = script.Parent
local Player
local Char
local Humanoid
local IdleAnimation
local SwingAnimation
Tool.Equipped:Connect(function()
Player = game.Players:GetPlayerFromCharacter(Tool.Parent)
Char = Player.Character
Humanoid = Char:WaitForChild("Humanoid")
local Animator = Humanoid:FindFirstChildOfClass("Animator")
if Animator then
IdleAnimation = Animator:LoadAnimation(script.Hold)
SwingAnimation = Animator:LoadAnimation(script.Slash)
IdleAnimation:Play()
end
end)
Tool.Activated:Connect(function()
if IdleAnimation then
IdleAnimation:Stop()
end
wait()
SwingAnimation:Play()
Tool.Blade.Trail.Enabled = true
Tool.Blade.Swing:Play()
local CanDamage = true
Tool.Blade.Touched:Connect(function(Touched)
local TouchedParent = Touched.Parent
local EnHumanoid = TouchedParent:FindFirstChildWhichIsA("Humanoid")
if EnHumanoid and EnHumanoid ~= Humanoid and CanDamage then
EnHumanoid:TakeDamage(math.random(20,50))
Tool.Blade.Hit:Play()
CanDamage = false
end
end)
SwingAnimation.Stopped:Wait()
Tool.Blade.Trail.Enabled = false
IdleAnimation:Play()
CanDamage = false
end)
Tool.Unequipped:Connect(function()
IdleAnimation:Stop()
SwingAnimation:Destroy()
IdleAnimation:Destroy()
end)
Your code looks clean and I can understand everything you are trying to do. Unfortunately, it would be difficult to tell what it can cause it. I imagine it is either the animations or the parts of the sword itself. Perhaps the item you created, which looks like some sort of melee weapon, has too many rendering parts (lots of meshes perhaps).
It could also be other scripts within the said weapon.
My suggestion is to test the tool by replacing the whole model with a simple block and see if it lags it. If that doesn’t fix the problem then try to run it without animations, especially the “Idle” one.
you’re making a Touched connection everytime the tool is activated, this connection is never disconnected and would last forever until the tool is destroyed, you can fix this by moving the touched connection outside of the activation scope
local CanDamage = false
Tool.Activated:Connect(function()
if IdleAnimation then
IdleAnimation:Stop()
end
wait()
SwingAnimation:Play()
Tool.Blade.Trail.Enabled = true
Tool.Blade.Swing:Play()
CanDamage = true
SwingAnimation.Stopped:Wait()
Tool.Blade.Trail.Enabled = false
IdleAnimation:Play()
CanDamage = false
end)
Tool.Blade.Touched:Connect(function(Touched)
if CanDamage then
local TouchedParent = Touched.Parent
local EnHumanoid = TouchedParent:FindFirstChildWhichIsA("Humanoid")
if EnHumanoid and EnHumanoid ~= Humanoid then
EnHumanoid:TakeDamage(math.random(20,50))
Tool.Blade.Hit:Play()
CanDamage = false
end
end
end)