I’m working on a game that has an injure function, carry function, and execute function in my characterhandler script which used to work fine. I don’t know when it started, but these functions started causing immense amounts of lag especially when spammed. The ping reaches up to 1000ms in these cases and goes back down a couple seconds after it’s stopped, but the rate does not slow down. It’s my first time genuinely scripting on roblox so I don’t know exactly how to prevent lag, I’ve made sure that most loops aren’t left running for no reason and everything is deleted after it’s done being used so I’m not sure what could be causing this.
function injure()
if not actionCheck() and not character:FindFirstChild("Injuring") and not character:FindFirstChild("Gripping") and not character:FindFirstChild("Carrying") then
local target
target = sortKnocked()
if target ~= nil and liveFolder[target]:FindFirstChild("Knocked") and liveFolder[target].Humanoid.Health ~= 0 then
local gettingInjuredTrack = liveFolder[target].Humanoid.Animator:LoadAnimation(Anims["BeingGripped"])
gettingInjuredTrack.Priority = Enum.AnimationPriority.Idle
gettingInjuredTrack.Looped = true
if liveFolder[target]:FindFirstChild("Knocked") and liveFolder[target].Humanoid.Health ~= 0 then
if not character:FindFirstChild("HeavyStunned") then
local heavyStun = Instance.new("Folder")
heavyStun.Name = "HeavyStunned"
heavyStun.Parent = character
end
if not character:FindFirstChild("Injuring") then
local injureFolder = Instance.new("Folder")
injureFolder.Name = "Injuring"
injureFolder.Parent = character
end
if not character:FindFirstChild("BeingInjured") then
local beingInjuredFolder = Instance.new("Folder")
beingInjuredFolder.Name = "BeingInjured"
beingInjuredFolder.Parent = liveFolder[target]
end
if not liveFolder[target]:FindFirstChild("HeavyStunned") then
local heavyStun = Instance.new("Folder")
heavyStun.Name = "HeavyStunned"
heavyStun.Parent = liveFolder[target]
end
liveFolder[target]:FindFirstChild("Knocked"):Destroy()
liveFolder[target].HumanoidRootPart.CFrame = CFrame.new(liveFolder[target].HumanoidRootPart.Position.X, character.HumanoidRootPart.Position.Y, liveFolder[target].HumanoidRootPart.Position.Z)
liveFolder[target].HumanoidRootPart.Orientation = Vector3.new(0, liveFolder[target].HumanoidRootPart.Orientation.Y, 0)
gettingInjuredTrack:Play()
injureTrack:Play()
character.HumanoidRootPart.CFrame = CFrame.new(liveFolder[target].HumanoidRootPart.Position)
character.HumanoidRootPart.CFrame = CFrame.lookAt(character.HumanoidRootPart.Position, liveFolder[target].HumanoidRootPart.CFrame.Position + -(liveFolder[target].HumanoidRootPart.CFrame.LookVector * 10))
local injureConstraint = Instance.new("AlignPosition", character.HumanoidRootPart)
injureConstraint.Name = "InjureForce"
injureConstraint.Attachment0 = character.HumanoidRootPart.RootAttachment
injureConstraint.Attachment1 = liveFolder[target].HumanoidRootPart.RootAttachment
injureConstraint.RigidityEnabled = false
liveFolder[target].HumanoidRootPart.CFrame = CFrame.new(liveFolder[target].HumanoidRootPart.Position.X, character.HumanoidRootPart.Position.Y, liveFolder[target].HumanoidRootPart.Position.Z)
liveFolder[target].HumanoidRootPart.Orientation = Vector3.new(0, liveFolder[target].HumanoidRootPart.Orientation.Y, 0)
humanoid.AutoRotate = false
local function stop()
injureConstraint:Destroy()
humanoid.AutoRotate = true
if injureTrack.IsPlaying then
injureTrack:Stop()
end
if gettingInjuredTrack.IsPlaying then
gettingInjuredTrack:Stop()
end
if character:FindFirstChild("Injuring") then
character.Injuring:Destroy()
end
if character:FindFirstChild("HeavyStunned") then
character.HeavyStunned:Destroy()
end
if liveFolder[target]:FindFirstChild("HeavyStunned") then
liveFolder[target].HeavyStunned:Destroy()
end
if liveFolder[target]:FindFirstChild("BeingInjured") then
liveFolder[target].BeingInjured:Destroy()
end
if not liveFolder[target]:FindFirstChild("Knocked") then
local knock = Instance.new("Folder")
knock.Name = "Knocked"
knock.Parent = liveFolder[target]
game:GetService("Debris"):AddItem(knock, 15)
end
return
end
character.ChildAdded:Connect(function(child)
if child.Name == "Stunned" then
if character:FindFirstChild("Injuring") then
character.Injuring:Destroy()
end
end
end)
game.Players.PlayerRemoving:Connect(function(leftPlayer)
if leftPlayer.Name == target then
if character:FindFirstChild("Injuring") then
character.Injuring:Destroy()
end
end
end)
character.ChildRemoved:Connect(function(child)
if child.Name == "Injuring" then
stop()
end
end)
injureTrack:GetMarkerReachedSignal("StartInjure"):Connect(function()
liveFolder[target].HumanoidRootPart.Bonebreak:Play()
local hit = liveFolder[target].HumanoidRootPart.Hit:Clone()
hit.Parent = liveFolder[target].Torso
hit.Enabled = true
game:GetService("Debris"):AddItem(hit, 0.3)
end)
injureTrack.Stopped:Connect(function()
tagTarget(target, 15)
stop()
end)
end
end
elseif character:FindFirstChild("Injuring") then
character.Injuring:Destroy()
end
end