So, basically, my tool which is a sword is lagging the game when I activate it (click the mouse button)
I don’t know is the script fault or the games fault (the game has a lot of parts)
I would start at a steady 60 fps but then go down to 30 after one click.
I have no idea why.
I tried deleting some stuff that I will not use anymore in the script (eg I don’t need the loaded animation, I delete it.)
Script / code:
local Tool = script.Parent
local Player
local Char
local Humanoid
local IdleAnimation
local SwingAnimation
Tool.Equipped:Connect(function()
print("equipped")
Player = game.Players:GetPlayerFromCharacter(Tool.Parent)
Char = Player.Character
Humanoid = Char:WaitForChild("Humanoid")
local Animator = Humanoid:FindFirstChildOfClass("Animator")
if Animator and Humanoid then
print("animator found")
elseif Humanoid then
Animator = Instance.new("Animator",Humanoid)
else
warn("how, how and why")
end
IdleAnimation = Animator:LoadAnimation(script.Hold)
SwingAnimation = Animator:LoadAnimation(script.Slash)
IdleAnimation:Play()
end)
Tool.Activated:Connect(function()
print("act")
print(IdleAnimation)
print(SwingAnimation)
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)
print("touch")
local TouchedParent = Touched.Parent
local EnHumanoid = TouchedParent:FindFirstChildWhichIsA("Humanoid")
if EnHumanoid and EnHumanoid ~= Humanoid and CanDamage then
print("damg")
EnHumanoid:TakeDamage(math.random(20,50))
Tool.Blade.Hit:Play()
CanDamage = false
end
end)
SwingAnimation.Stopped:Wait()
Tool.Blade.Trail.Enabled = false
IdleAnimation:Play()
print("end")
CanDamage = false
end)
Tool.Unequipped:Connect(function()
print("Unequipped")
IdleAnimation:Stop()
SwingAnimation:Destroy() -- in an attempt to remove lag
IdleAnimation:Destroy() -- in an attempt to remove lag
end)