Coin deletion causing lag

local Coin = script.Parent
local CoinTouched = false

Coin.Touched:Connect(function(hit)
	local HRP = hit.Parent:FindFirstChild('HumanoidRootPart')
	
	if HRP then
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		local leaderstats = plr:FindFirstChild('leaderstats')
		local CoinsStat = leaderstats.Coins

		CoinTouched = true
		CoinsStat.Value += 10
		Coin:Destroy()
	end
end)

The code works the way it is suppose to work, however when it gets to the Coin:Destroy() part it lags my game out for half a second. I know this because I replaced that line with Script.Enable = false and the lag didn’t seem to occur anymore. Does anyone know why this happens, and if possible give me a solution to this issue?

also unrelated, but just got back to relearning lua.

How complicated is the coin model? what is the hierarchy in it? Sounds like you are destroying something really complicated or something.

its just a 2 part union with an effect :broken_heart:

What effect? Could that be the source of the issue? Deleting a union should be relatively cheap and something you shouldn’t notice if you are deleting one at a time.

you never use the cointouched variable, maybe its running multiple times? (idk)

well it wasn’t the effect, it was the sparkle effect and I removed it rn to test it and got the same result.

I think it may be that the coin doesn’t have a touch cooldown, so when the player is touching it, the script continuously fires the touch function?

idek why that there anymore. anyways I removed it and still have the same issue.

Are there other scripts that reference this in any way? Such as a spawner that is placing new coins?

I meant to make it work, just make it return if its already “touched”

no, its just a coin I have sitting just to test and see if it works.

That’s what I was about to say, so maybe replace “if HRP then” to “if HRP and CoinTouched == false then”

I already tried debounce, it didnt make a difference.

Honestly I’m out of ideas as to why this could be happening. I have a few questions that might help, but I kind of doubt they will.

Does your game lag out if you just remove the destroy line without disabling the script?

Also what if you delete the coin from another script after a couple seconds without triggering touch?

What if you replace the coin with a single part?

As for a potential workaround you can try object pooling. Never delete the coin but carefully manage a collection of them and just set them up and deconstruct the script stuff as you go.

found the problem, it’s that your placing your script into the coin. Place it in serverscriptstorage and test if it works. If it does (which it did in my case) Use collection service so you can use the code on more then one coin

Heres the code

local CollectionService = game:GetService("CollectionService")
local Tag = "Coin"

local function Coin(CoinModel)
	Coin.Touched:Connect(function(hit)
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if not plr then return end
		Coin:Destroy()
	end)
end

CollectionService:GetInstanceAddedSignal(Tag):Connect(Coin)

for i,v in ipairs(CollectionService:GetTagged(Tag)) do
	Coin(v)
end
1 Like

that doesn’t cause lag normally, that isn’t “the problem,” but its defiantly a guess at least to some freak edge case here.

well just moving the script into serverscriptstorage didn’t change anything, but amma try the code you gave me.

im still yet to learn this service so i guess this a cool introduction to it.

btw i did a little typo in that code

replace “Coin.Touched” and “Coin:Destroy()” with CoinModel

you got any videos of it lagging?

Heres me copying your code into my own game

and heres the code i made