My script seems to cause lag after a minute or so and i dont know how to fix it (pretty much new to scripting)

I would love if someone could explain to me what i did wrong in this script to make it cause lag to the server

  1. What is the issue? Include screenshots / videos if possible!
    local Human = game.Players.LocalPlayer
    local char = Human.CharacterAdded:Wait()
    local WeaponTool = script.Parent
    local man = char:WaitForChild(“Humanoid”)
    local idle = man:LoadAnimation(script:WaitForChild(“Equip”))
    local M1 = man:LoadAnimation(script:WaitForChild(“m1”))
    local M2 = man:LoadAnimation(script:WaitForChild(“m2”))
    local M3 = man:LoadAnimation(script:WaitForChild(“m3”))
    local M4 = man:LoadAnimation(script:WaitForChild(“m4”))
    local bebe = false – Debounce
    local combo = 0
    local HitPlayer = {}
    local DamageA = script.Script.RemoteEvent
    local DamageC = script.M4.Lhit
    local TUCR = 1.5
    local LTC = tick()

script.Parent.Equipped:Connect(function()
game.ReplicatedStorage.ConnectM6D:FireServer(WeaponTool.BodyAttach)
char.Torso.ToolGrip.Part0 = char[“Right Arm”]
char.Torso.ToolGrip.Part1 = WeaponTool.BodyAttach
idle:Play()
end)

WeaponTool.Activated:Connect(function()
if char:GetAttribute(“Blocking”) == true then return end
if char:GetAttribute(“BlockBroken”) == true then return end
if char:FindFirstChild(“Stun”) then return end
if char:GetAttribute(“NoM1”) == true then return end
if char:GetAttribute(“Ability”) == true then return end

		if not bebe then
			bebe = true
			if tick() - LTC >= TUCR then -- combo resets after not clicking for a while
				combo = 0
			end
			LTC = tick()
			if combo == 0  then
				M1:Play()
				wait(0.25)  -- time before hitbox spawns in
				DamageA:FireServer()
				combo = combo + 1
				wait(0.18) -- to make sure animation ends before starting the next anim/slash
				bebe = false
			elseif combo == 1 then
				M2:Play()
				wait(0.25)
				DamageA:FireServer()
				combo = combo + 1
				wait(0.18)
				bebe = false
			elseif combo == 2 then
				M3:Play()
				wait(0.25)
				DamageA:FireServer()
				combo = combo + 1
				wait(0.18)
				bebe = false
			elseif combo == 3 then
				M4:Play()
				wait(0.25)
				DamageC:FireServer()
				combo = combo + 1
				wait(0.7)
				bebe = false
				combo = 0

			end
		end
end)

script.Parent.Unequipped:Connect(function()
game.ReplicatedStorage.DisconnectM6D:FireServer()
idle:Stop()
end)

i havent been able to come up with any kind of solution so i decided to post it here

I beautified the script so it’s a lot easier to read, however I don’t think your bug is fixed yet:

local player = game.Players.LocalPlayer
local char = player.CharacterAdded:Wait()
local WeaponTool = script.Parent
local hum = char:WaitForChild("Humanoid")
local idle = hum:LoadAnimation(script:WaitForChild("Equip"))

local M1 = hum:LoadAnimation(script:WaitForChild("m1"))
local M2 = hum:LoadAnimation(script:WaitForChild("m2"))
local M3 = hum:LoadAnimation(script:WaitForChild("m3"))
local M4 = hum:LoadAnimation(script:WaitForChild("m4"))

local DamageA = script.Script.RemoteEvent
local DamageC = script.M4.Lhit

local debounce = false
local combo = 0
local HitPlayer = {}

local combotimewindow = 1.5
local lastclick = tick()

script.Parent.Equipped:Connect(function()
	game.ReplicatedStorage.ConnectM6D:FireServer(WeaponTool.BodyAttach)
	char.Torso.ToolGrip.Part0 = char["Right Arm"]
	char.Torso.ToolGrip.Part1 = WeaponTool.BodyAttach
	idle:Play()
end)

WeaponTool.Activated:Connect(function()
	local stats = char:GetAttributes()
	if stats.Blocking or stats.BlockBroken then return end
	if char:FindFirstChild("Stun") then return end
	if stats.NoM1 then return end
	if stats.Ability then return end

	if not debounce then
		debounce = true
		if tick() - lastclick >= combotimewindow then -- combo resets after not clicking for a while
			combo = 0
		end
		lastclick = tick()
		if combo == 0  then
			M1:Play()
			task.wait(0.25)  -- time before hitbox spawns in
			DamageA:FireServer()
			combo = combo + 1
			task.wait(0.18) -- to make sure animation ends before starting the next anim/slash
			debounce = false
		elseif combo == 1 then
			M2:Play()
			task.wait(0.25)
			DamageA:FireServer()
			combo += 1
			task.wait(0.18)
			debounce = false
		elseif combo == 2 then
			M3:Play()
			task.wait(0.25)
			DamageA:FireServer()
			combo += 1
			task.wait(0.18)
			debounce = false
		elseif combo == 3 then
			M4:Play()
			task.wait(0.25)
			DamageC:FireServer()
			combo += 1
			task.wait(0.7)
			debounce = false
			combo = 0
		end
	end
end)

script.Parent.Unequipped:Connect(function()
    game.ReplicatedStorage.DisconnectM6D:FireServer()
    idle:Stop()
end)

Where is this script located? In a tool I’m guessing?

I not sure but i think it cause playing animation or firing remote, may your just lagged cause your device.

ye its inside the tool, so far the whole problem really just is if i use the weapon when just spawned it works fine but over time it just becomes a lagging mess

Can you show a video of the lagging? What type of lag do you mean?

this would be it feel like the video doesnt quite capture the lag that well but its basically just slows everything down a bit

Could you try finding out what is causing the lag using the microprofiler?
How to use microprofiler:

only been seeing heartbeat being referenced a lot not sure if could be of 2 scripts that run heartbeat which are
script1:

local player = game.Players.LocalPlayer

local Char = player.Character

local NormalWalkSpeed = 16
local Sprinting = NormalWalkSpeed * 2
local NormalJumpPower = 50

game:GetService(“RunService”).Heartbeat:Connect(function()

if Char:FindFirstChild("Stun") then
	Char:FindFirstChild("Humanoid").JumpPower = 0
	Char:FindFirstChild("Humanoid").WalkSpeed = 1
	return
end

if Char:FindFirstChild("Stop") then
	Char:FindFirstChild("Humanoid").JumpPower = 0
	Char:FindFirstChild("Humanoid").WalkSpeed = 0
	return
end

if Char:GetAttribute("Blocking") == true then
	Char:FindFirstChild("Humanoid").JumpPower = 50
	Char:FindFirstChild("Humanoid").WalkSpeed = 2
end

if Char:GetAttribute("BlockBroken") == true then
	Char:FindFirstChild("Humanoid").JumpPower = 0
	Char:FindFirstChild("Humanoid").WalkSpeed = 0
end

if  Char:GetAttribute("Blocking") == false and Char:FindFirstChild("Stun") == nil and Char:GetAttribute("BlockBroken") == false and Char:FindFirstChild("Stop") == nil then
	Char:FindFirstChild("Humanoid").JumpPower = NormalJumpPower
	Char:FindFirstChild("Humanoid").WalkSpeed = NormalWalkSpeed
end

end)

script2:

local damage = Instance.new(“IntValue”)

local run = game:GetService(“RunService”)

local Char = script.Parent

script.Parent.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Ragdoll, false)

script.Parent:SetAttribute(“Blocking”, false)

script.Parent:SetAttribute(“BlockBroken”, false)

script.Parent:SetAttribute(“NoM1”, false)

script.Parent:SetAttribute(“IFrames”, false)

script.Parent:SetAttribute(“Ability”, false)

script.Parent:SetAttribute(“buff”, false)

script.Parent:SetAttribute(“Mode”,false)

script.Parent:WaitForChild(“Humanoid”).UseJumpPower = true

– med weapons

damage.Parent = script.Parent

damage.Name = “damage”

run.Heartbeat:Connect(function()

if Char:FindFirstChild(“damage”) then

if Char:GetAttribute(“buff”) == true then

Char:FindFirstChild(“damage”).Value = 6

end

end

if Char:FindFirstChild(“damage”) then

if Char:GetAttribute(“buff”) == false then

Char:FindFirstChild(“damage”).Value = 4

end

end

end)

You can send a script properly like this…

print("Hello")

image

script 1:

local player = game.Players.LocalPlayer

local Char = player.Character

local NormalWalkSpeed = 16
local Sprinting = NormalWalkSpeed * 2
local NormalJumpPower = 50

game:GetService("RunService").Heartbeat:Connect(function()
	
	if Char:FindFirstChild("Stun") then
		Char:FindFirstChild("Humanoid").JumpPower = 0
		Char:FindFirstChild("Humanoid").WalkSpeed = 1
		return
	end
	
	if Char:FindFirstChild("Stop") then
		Char:FindFirstChild("Humanoid").JumpPower = 0
		Char:FindFirstChild("Humanoid").WalkSpeed = 0
		return
	end
	
	if Char:GetAttribute("Blocking") == true then
		Char:FindFirstChild("Humanoid").JumpPower = 50
		Char:FindFirstChild("Humanoid").WalkSpeed = 2
	end
	
	if Char:GetAttribute("BlockBroken") == true then
		Char:FindFirstChild("Humanoid").JumpPower = 0
		Char:FindFirstChild("Humanoid").WalkSpeed = 0
	end
	
	if  Char:GetAttribute("Blocking") == false and Char:FindFirstChild("Stun") == nil and Char:GetAttribute("BlockBroken") == false and Char:FindFirstChild("Stop") == nil then
		Char:FindFirstChild("Humanoid").JumpPower = NormalJumpPower
		Char:FindFirstChild("Humanoid").WalkSpeed = NormalWalkSpeed
	end
	
	
	
	
	
	
end)

script 2

local damage = Instance.new("IntValue")
local run = game:GetService("RunService")
local Char = script.Parent
script.Parent.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Ragdoll, false)
script.Parent:SetAttribute("Blocking", false)
script.Parent:SetAttribute("BlockBroken", false)
script.Parent:SetAttribute("NoM1", false)
script.Parent:SetAttribute("IFrames", false)
script.Parent:SetAttribute("Ability", false)
script.Parent:SetAttribute("buff", false)
script.Parent:SetAttribute("Mode",false)
script.Parent:WaitForChild("Humanoid").UseJumpPower = true
-- med weapons
damage.Parent = script.Parent
damage.Name = "damage"


run.Heartbeat:Connect(function()
	if Char:FindFirstChild("damage") then
		if Char:GetAttribute("buff") == true then
			Char:FindFirstChild("damage").Value = 6
		end
	end

	if Char:FindFirstChild("damage") then
		if Char:GetAttribute("buff") == false then
			Char:FindFirstChild("damage").Value = 4
		end
	end
end)

Do you think it could be overlapping play() without stopping the one playing. Maybe heartbeat is going too fast in this case. step is every fps … heartbeat is as fast as possible. And you do mave many task.wait(). Lot of studdering a over powered loop (just a guess).

thank you very much because if you now i know that script 1 is the cause of lag as you said it goes to fast and for some reason every time a player clicks it completely resets causing more lag

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.