Why is this script tanking fps so dramatically (60 fps to 3 fps)

this is the whole script:

stand:WaitForChild("Sword").Touched:Connect(function(p)
	print("hit", p)
	if dmg == 0 then return end
	if sdb then return end
	if p.Parent:FindFirstChild("Humanoid") then
		if p.Parent == chr then
			return
		end
		if swordequipped then
			if sdb then return end
			sdb = true
			print("swordhit", p)
			game.ReplicatedStorage.AddHitmarkerClient:FireServer(chr,p.Parent.Humanoid, dmg, knock, knockvalue)
			script.SlashSounds.randomhitsound:FireServer()
			if knock then
				game.ReplicatedStorage.Knock:FireServer(p.Parent.Humanoid)
			end
			coroutine.resume(coroutine.create(function()
				task.wait(.1)
				sdb = false
			end))
		end
	end
end)

and this is what happens
https://i.gyazo.com/591c2e6bd8f6b3ce0d8acf6100217906.mp4

i notice the draw total and draw shadow are extremely high when it gets touched (specifically the tri value) does that have anything to do with it?

it also only happens on players specifically

https://i.gyazo.com/cd98bd38fab3d92c6e1f63c5300495cc.mp4

1 Like

what does the output say, idk yet but i am pretty sure .touched is just firing too much and your debounce is not working properly.

instead of using a coroutine and yielding for tracking debounces, try using tick() or os.clock() to save the time it hit, and use an if statement to see if it should be allowed to hit again. if you need a code representation just ask and ill create an example.

3 Likes

yea i need some help for this
i dont understand anything related to that

here’s a little example of what i mean by time based debounces

local timeHit = 0
local hitcooldown = 2
local override = false

-- inside some hit event,

if os.clock() - timeHit >= hitcooldown then
  if not override then
    timeHit = os.clock()
    -- do stuff
  end
end

I added an override bool just incase you want to disable something even if the timer has ended.

1 Like

The lag spikes you are experiencing are mainly caused by this part in your code…

coroutine.resume(coroutine.create(function()
	task.wait(.1)
	sdb = false
end))

Right here you are constantly creating, yielding and unyielding a coroutine. The same sort of thing happens when while wait() do print("whatever") end is done. This is bad practice and puts a lot of load on ROBLOX’s computation.
Look into @MysteryOfHyper’s debounce suggestion.

1 Like

Ok so it appears something weird is happening, i used your solution and it DOES work and improve performance, but that wasn’t the full issue. The issue is actually with these 2 meshes

with:
https://i.gyazo.com/512c676e649d818ec91f032f2cde1df6.mp4

without:
https://i.gyazo.com/cc275936c5a7c8cf836aca99ff93da53.mp4

(im using adonis admin’s built in dex command)

edit:
disabling .touched for those orbs worked along with ur fix but i still want to know why it was doing that

1 Like

If the issue is with those meshes then I don’t know. :sad: I can only guess the meshes are just really laggy and have too many triangles.

the issue was with the meshes but still thank you for the new debounce system because it does work way better

1 Like