Touched event, extreme lag

Well, just try the debounce system like this

if debounce == false then
debounce = true
-- run below, 

You are basically checking if it is not debounce, which it always will be; making it run infinitely.

Continuing, you are basically doing everything a massive amount of times; hence the lag.

1 Like

already tried, stop trying to help me with debounce this is really not the problem now

1 Like

Your code is literally made to make it run infinitely on touched. That is why it is lagging. Try it out.

1 Like

you know that a debounce does not cause the touched event to stop be fired right?

1 Like

The problem is likely with your Debounce; the infinite wait is useless and as you’re doing it before any debouncing, the Touched is constantly triggering new threads. Part of the problem is that you’re using an anonymized function that has to be re-compiled every time it is run. The other (and arguably worse) part is that you are only triggering the debounce when hitting “Chao” instead of always, defeating the purpose of de-bouncing the function.

Try this:

	local Debounce = false

	local touchedConn -- this is done so that the function can disconnect itself.
	function touched(QuemTocou)
		if Debounce then return end
		Debounce = true

		if QuemTocou.Name == "Chao" then
			touchedConn:Disconnect()
			wait(1)
			LightBombClone.Anchored = true
			Efeito1Clonado.Parent = workspace
			Efeito1Clonado.Position = LightBombClone.Position + Vector3.new(0,-20,0)
			Efeito2Clonado.AnimationScript.Disabled = false
			char.CameraShake.Disabled = false
			wait(7)
			for y = 0,1,0.1 do
				wait(0.1)
				LightBombClone.Transparency = y
			end
			char.CameraShake.Disabled = true
			LightBombClone:Destroy()
			Efeito1Clonado:Destroy()
			Efeito2Clonado:Destroy()
		else
            Debounce = false
        end
	end
	touchedConn = LightBombClone.Touched:Connect(touched)
2 Likes

maybe your theory is correct but …

image

1 Like

My bad, did something in the wrong order, one moment…

I’ve edited my post, try it now!

1 Like

this is normal?

image

1 Like

Why wouldn’t it be? It’s just a named function. (Although, another mistake, you should put local at the start of that line so this doesn’t break if two people use it at the same time.)

1 Like

Still with the lag, no errors in the output

1 Like

this isn’t that weird? or are we missing something?

1 Like

Can you put print statements inside each sub-functions and between every wait to see if anything starts spamming the output?

1 Like

Okay, give me just a second, i’ll do it

1 Like

why don’t you do it your way? I can do it the wrong way than you think

1 Like

Because 1) I don’t know the context for what you’re trying to do here and 2) I’m trying to help you learn how to solve this type of problem, as I don’t offhand know what the problem or its potential solutions are.

1 Like

I rlly, rlly, rlly, rlly don’t know where to put more prints

1 Like

it seems that you have noticed a lot, do you know how to solve it or do you need more prints?

1 Like

I don’t know the source of the lag, no.

I’m asking for more prints as a way for you to try and figure out what part of the script is causing the lagspike. Before and after every wait is generally where I’d put them.

Alternatively, you could use breakpoints and step through the script in the debugger for the same effect.

1 Like

I will put prints in the entire code then

1 Like

SCRIPT:



script.Parent.LightBombCriarPoderEvent.OnServerEvent:Connect(function(Player)
print("OutTest1")
local ReplicatedStorage = game.ReplicatedStorage
	
local char = Player.Character or Player.CharacterAdded:Wait()
local Ff = Instance.new("ForceField")
Ff.Parent = char:WaitForChild("Humanoid")
Ff.Visible = false
print("OutTest2")
ReplicatedStorage.Nome.Value = char.Name

local root = char:WaitForChild("HumanoidRootPart")
local Humanoid = char:WaitForChild("Humanoid")
local Debounce = false
	
local LightBomb = ReplicatedStorage:WaitForChild("LightBomb")
local Efeito1 = ReplicatedStorage:WaitForChild("Particula")
local Efeito2 = ReplicatedStorage:WaitForChild("Efeito2")
	print("OutTest3")
local LightBombClone = LightBomb:Clone()
local Efeito1Clonado = Efeito1:Clone()
local Efeito2Clonado = Efeito2:Clone()
	
for _, playingTracks in pairs(Humanoid:GetPlayingAnimationTracks()) do
		playingTracks:Stop()
		print("OutTest4")
end



							--[[
									char.HumanoidRootPart.Anchored = true

									wait(0.05)

									char.HumanoidRootPart.Anchored = false
							--]]

local bv = Instance.new("BodyVelocity", char.PrimaryPart)
bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
bv.Velocity = Vector3.new(0,13,0)
bv.Name = "FlightForce"
	print("OutTest5")

	wait(3.54)
	print("OutTest6")
bv.Velocity = Vector3.new(0,0,0)
-- 11.44 = levantar mão
	wait(7.9)
	print("OutTest7")
	Efeito2Clonado.Position = LightBombClone.Position
	LightBombClone.Parent = workspace

	LightBombClone.Anchored = true
	
	Efeito1Clonado.Efeito1.Enabled = true
	
	Efeito2Clonado.Parent = workspace
	wait()
	local weld = Instance.new("WeldConstraint")
	weld.Part0 = Efeito2Clonado
	weld.Part1 = LightBombClone
	weld.Parent = Efeito2Clonado
	
	spawn(function()
		for x = 4,45,1 do
			wait(0.01)
			LightBombClone.Position = char.Head.Position + Vector3.new(0,x,0)
			Efeito2Clonado.Position = char.Head.Position + Vector3.new(0,x,0)
			print("OutTest8")
		end
		print("OutTest9")
	end)

	spawn(function()
		for i = 1,60,1 do
			wait(0.01)
			LightBombClone.Size = Vector3.new(i,i,i)
			Efeito2Clonado.Size = Vector3.new(i,i,i)
			print("OutTest10")
		end	
		print("OutTest11")
	end)

	wait(8.56)
	print("OutTest12")
	local BV = Instance.new("BodyVelocity")
	BV.Name = "VelocidadeDoPoder"
	BV.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	BV.Parent = Efeito2Clonado
	script.Parent.MousePosition.Disabled = false
	script.Parent.MouseHitEvent.OnServerEvent:Connect(function(plr, Mouse)
		BV.Velocity = CFrame.new(LightBombClone.Position, Mouse).LookVector * 32
		print("OutTest13")		
	end)
	print("OutTest14")
	LightBombClone.Anchored = false

	local Debounce = false

	local touchedConn -- this is done so that the function can disconnect itself.
	local function touched(QuemTocou)
		print("Test1")
		if Debounce then return end
		print("Test2")
		Debounce = true

		if QuemTocou.Name == "Chao" then
			print("Test3")
			touchedConn:Disconnect()
			print("Test4")
			wait(1)
			LightBombClone.Anchored = true
			Efeito1Clonado.Parent = workspace
			Efeito1Clonado.Position = LightBombClone.Position + Vector3.new(0,-20,0)
			Efeito2Clonado.AnimationScript.Disabled = false
			char.CameraShake.Disabled = false
			wait(7)
			for y = 0,1,0.1 do
				wait(0.1)
				LightBombClone.Transparency = y
			end
			char.CameraShake.Disabled = true
			LightBombClone:Destroy()
			Efeito1Clonado:Destroy()
			Efeito2Clonado:Destroy()
		else
			print("Test5")
			Debounce = false
		end
	end
	touchedConn = LightBombClone.Touched:Connect(touched)
	
	
wait(10)
--11.48 jogou
Ff:Destroy()
script.Parent.MousePosition.Disabled = true
print("Caiu")
char.Animate.Disabled = false

bv:Destroy()
--Humanoid:LoadAnimation(script.FallPose):Play()
end)

RESULTS:

23:24:03.743 OutTest1 - Server - Script:4
23:24:03.763 OutTest2 - Server - Script:11
23:24:03.763 OutTest3 - Server - Script:21
23:24:03.764 OutTest4 - Server - Script:28
23:24:03.787 OutTest5 - Server - Script:45
23:24:07.369 OutTest6 - Server - Script:48
23:24:15.284 OutTest7 - Server - Script:52
23:24:15.486 OutTest8 - Server - Script:72
23:24:15.486 OutTest10 - Server - Script:82
23:24:15.530 OutTest8 - Server - Script:72
23:24:15.531 OutTest10 - Server - Script:82
23:24:15.587 OutTest8 - Server - Script:72
23:24:15.588 OutTest10 - Server - Script:82
23:24:15.685 OutTest8 - Server - Script:72
23:24:15.685 OutTest10 - Server - Script:82
23:24:15.766 OutTest8 - Server - Script:72
23:24:15.766 OutTest10 - Server - Script:82
23:24:15.844 OutTest8 - Server - Script:72
23:24:15.844 OutTest10 - Server - Script:82
23:24:15.886 OutTest8 - Server - Script:72
23:24:15.887 OutTest10 - Server - Script:82
23:24:15.923 OutTest8 - Server - Script:72
23:24:15.923 OutTest10 - Server - Script:82
23:24:15.965 OutTest8 - Server - Script:72
23:24:15.965 OutTest10 - Server - Script:82
23:24:16.009 OutTest8 - Server - Script:72
23:24:16.010 OutTest10 - Server - Script:82
23:24:16.056 OutTest8 - Server - Script:72
23:24:16.057 OutTest10 - Server - Script:82
23:24:16.102 OutTest8 - Server - Script:72
23:24:16.105 OutTest10 - Server - Script:82
23:24:16.152 OutTest8 - Server - Script:72
23:24:16.153 OutTest10 - Server - Script:82
23:24:16.209 OutTest8 - Server - Script:72
23:24:16.209 OutTest10 - Server - Script:82
23:24:16.254 OutTest8 - Server - Script:72
23:24:16.254 OutTest10 - Server - Script:82
23:24:16.291 OutTest8 - Server - Script:72
23:24:16.292 OutTest10 - Server - Script:82
23:24:16.337 OutTest8 - Server - Script:72
23:24:16.337 OutTest10 - Server - Script:82
23:24:16.384 OutTest8 - Server - Script:72
23:24:16.384 OutTest10 - Server - Script:82
23:24:16.427 OutTest8 - Server - Script:72
23:24:16.429 OutTest10 - Server - Script:82
23:24:16.477 OutTest8 - Server - Script:72
23:24:16.477 OutTest10 - Server - Script:82
23:24:16.524 OutTest8 - Server - Script:72
23:24:16.524 OutTest10 - Server - Script:82
23:24:16.555 OutTest8 - Server - Script:72
23:24:16.555 OutTest10 - Server - Script:82
23:24:16.601 OutTest8 - Server - Script:72
23:24:16.601 OutTest10 - Server - Script:82
23:24:16.642 OutTest8 - Server - Script:72
23:24:16.642 OutTest10 - Server - Script:82
23:24:16.686 OutTest8 - Server - Script:72
23:24:16.687 OutTest10 - Server - Script:82
23:24:16.733 OutTest8 - Server - Script:72
23:24:16.733 OutTest10 - Server - Script:82
23:24:16.781 OutTest8 - Server - Script:72
23:24:16.782 OutTest10 - Server - Script:82
23:24:16.829 OutTest8 - Server - Script:72
23:24:16.830 OutTest10 - Server - Script:82
23:24:16.875 OutTest8 - Server - Script:72
23:24:16.876 OutTest10 - Server - Script:82
23:24:16.922 OutTest8 - Server - Script:72
23:24:16.947 OutTest10 - Server - Script:82
23:24:16.967 OutTest8 - Server - Script:72
23:24:16.994 OutTest10 - Server - Script:82
23:24:17.013 OutTest8 - Server - Script:72
23:24:17.038 OutTest10 - Server - Script:82
23:24:17.063 OutTest8 - Server - Script:72
23:24:17.089 OutTest10 - Server - Script:82
23:24:17.111 OutTest8 - Server - Script:72
23:24:17.138 OutTest10 - Server - Script:82
23:24:17.157 OutTest8 - Server - Script:72
23:24:17.183 OutTest10 - Server - Script:82
23:24:17.204 OutTest8 - Server - Script:72
23:24:17.231 OutTest10 - Server - Script:82
23:24:17.253 OutTest8 - Server - Script:72
23:24:17.279 OutTest10 - Server - Script:82
23:24:17.306 OutTest8 - Server - Script:72
23:24:17.333 OutTest10 - Server - Script:82
23:24:17.355 OutTest8 - Server - Script:72
23:24:17.383 OutTest10 - Server - Script:82
23:24:17.403 OutTest8 - Server - Script:72
23:24:17.429 OutTest10 - Server - Script:82
23:24:17.449 OutTest8 - Server - Script:72
23:24:17.479 OutTest10 - Server - Script:82
23:24:17.502 OutTest8 - Server - Script:72
23:24:17.502 OutTest9 - Server - Script:74
23:24:17.530 :arrow_forward: OutTest10 (x19) - Server - Script:82
23:24:18.387 OutTest11 - Server - Script:84
23:24:23.948 OutTest12 - Server - Script:88
23:24:23.948 OutTest14 - Server - Script:98
23:24:24.024 OutTest13 - Server - Script:96
23:24:24.469 Test1 - Server - Script:105
23:24:24.469 Test2 - Server - Script:107
23:24:24.470 Test5 - Server - Script:130
23:24:24.470 Test1 - Server - Script:105
23:24:24.470 Test2 - Server - Script:107
23:24:24.470 Test5 - Server - Script:130
23:24:24.485 Test1 - Server - Script:105
23:24:24.485 Test2 - Server - Script:107
23:24:24.485 Test5 - Server - Script:130
23:24:24.506 Test1 - Server - Script:105
23:24:24.506 Test2 - Server - Script:107
23:24:24.507 Test5 - Server - Script:130
23:24:24.507 Test1 - Server - Script:105
23:24:24.507 Test2 - Server - Script:107
23:24:24.508 Test5 - Server - Script:130
23:24:24.508 Test1 - Server - Script:105
23:24:24.508 Test2 - Server - Script:107
23:24:24.508 Test5 - Server - Script:130
23:24:24.508 Test1 - Server - Script:105
23:24:24.508 Test2 - Server - Script:107
23:24:24.508 Test5 - Server - Script:130
23:24:24.547 Test1 - Server - Script:105
23:24:24.547 Test2 - Server - Script:107
23:24:24.548 Test5 - Server - Script:130
23:24:24.548 Test1 - Server - Script:105
23:24:24.548 Test2 - Server - Script:107
23:24:24.548 Test5 - Server - Script:130
23:24:24.548 Test1 - Server - Script:105
23:24:24.548 Test2 - Server - Script:107
23:24:24.548 Test5 - Server - Script:130
23:24:24.548 Test1 - Server - Script:105
23:24:24.548 Test2 - Server - Script:107
23:24:24.548 Test5 - Server - Script:130
23:24:24.582 Test1 - Server - Script:105
23:24:24.583 Test2 - Server - Script:107
23:24:24.583 Test5 - Server - Script:130
23:24:24.583 Test1 - Server - Script:105
23:24:24.583 Test2 - Server - Script:107
23:24:24.584 Test5 - Server - Script:130
23:24:24.584 Test1 - Server - Script:105
23:24:24.584 Test2 - Server - Script:107
23:24:24.584 Test5 - Server - Script:130
23:24:24.584 Test1 - Server - Script:105
23:24:24.585 Test2 - Server - Script:107
23:24:24.585 Test5 - Server - Script:130
23:24:24.665 Test1 - Server - Script:105
23:24:24.665 Test2 - Server - Script:107
23:24:24.665 Test5 - Server - Script:130
23:24:24.665 Test1 - Server - Script:105
23:24:24.665 Test2 - Server - Script:107
23:24:24.665 Test5 - Server - Script:130
23:24:24.665 Test1 - Server - Script:105
23:24:24.665 Test2 - Server - Script:107
23:24:24.665 Test5 - Server - Script:130
23:24:24.665 Test1 - Server - Script:105
23:24:24.666 Test2 - Server - Script:107
23:24:24.666 Test5 - Server - Script:130
23:24:26.986 Test1 - Server - Script:105
23:24:26.986 Test2 - Server - Script:107
23:24:26.986 Test3 - Server - Script:111
23:24:26.986 Test4 - Server - Script:113