Is this Roblox Engine Bug Or My Code Thats Causing Lag Spike

Hello everyone,
When script.Parent:Destroy() is run, it freezes the server and client for about half a second and then works normally. I have identified that when the script itself is destroyed, that is what causes the lag.
More strangely though, when I destroy the same script for a second time, no lag occurs. Here’s the script:

local rs = game:GetService("ReplicatedStorage")
local remoteEvents = rs:WaitForChild("RemoteEvents")
local collectItemRemote = remoteEvents:WaitForChild("CollectItem")

local dataScript = require(game:GetService("ServerScriptService"):WaitForChild("DataScript"))

script.Parent:WaitForChild("ClickDetector").MouseClick:Connect(function(plr)
	local plrGui = plr:WaitForChild("PlayerGui")
	local infoGui = plrGui:WaitForChild("InfoGUI")
	local infoLabel = infoGui:WaitForChild("InfoLabel")

	if dataScript.serverData['GameStage'] == 'Scavenge' then
		if #dataScript.plrData[plr]['Items'] < 3 then
			collectItemRemote:FireClient(plr, script.Parent.Name)
			table.insert(dataScript.plrData[plr]['Items'], script.Parent.Name)
			
			script.Parent:Destroy()
		else
			infoLabel.Text = 'Your inventory is full. Put your items in your cart!'
		end
	elseif dataScript.serverData['GameStage'] == 'Intro' then
		infoLabel.Text = 'You cant pick up items yet!'
		task.delay(1.5, function()
			infoLabel.Text = ''
		end)
	end
end)

If script.Parent has many descendants it would likely cause lag

1 Like

Tell me how many descendants is currently at script.Parent and does that add up when you play the game

1 Like

2 descendants including script

Do you have another script inside with loop?

1 Like

No only this script and clickdetector

Is that full script:

local rs = game:GetService("ReplicatedStorage")
local remoteEvents = rs:WaitForChild("RemoteEvents")
local collectItemRemote = remoteEvents:WaitForChild("CollectItem")

local dataScript = require(game:GetService("ServerScriptService"):WaitForChild("DataScript"))

script.Parent:WaitForChild("ClickDetector").MouseClick:Connect(function(plr)
	local plrGui = plr:WaitForChild("PlayerGui")
	local infoGui = plrGui:WaitForChild("InfoGUI")
	local infoLabel = infoGui:WaitForChild("InfoLabel")

	if dataScript.serverData['GameStage'] == 'Scavenge' then
		if #dataScript.plrData[plr]['Items'] < 3 then
			collectItemRemote:FireClient(plr, script.Parent.Name)
			table.insert(dataScript.plrData[plr]['Items'], script.Parent.Name)
			
			script.Parent:Destroy()
		else
			infoLabel.Text = 'Your inventory is full. Put your items in your cart!'
		end
	elseif dataScript.serverData['GameStage'] == 'Intro' then
		infoLabel.Text = 'You cant pick up items yet!'
		task.delay(1.5, function()
			infoLabel.Text = ''
		end)
	end
end)
1 Like

yes the script i showed above is the full script

Problem is very unlikely to be you server script maybe its local script can you share part of it where Client recives signal from Server like Remote.OnClientEvent ?

i commented remote event fireclient out and it still lagged

try

script.Parent:WaitForChild("ClickDetector").MouseClick:Connect(function(plr)
	script.Parent:Destroy()
end)

if it still lags then problem is not module if its don’t lag problem is module script

Sorry for the late reply, I tried that and it lagged as much as before.

Here’s how you can replicate this scenario:
Just paste this serverscript into a part:

script.Parent:WaitForChild("ClickDetector").MouseClick:Connect(function(plr)
	script.Parent:Destroy()
end)

And then add a clickdetector

1 Like

I have confirmed by doing this in another place that this is not an issue with clickdetector or anything.

Whenever you create a part and has a script inside it and you delete it, this causes a lag spike

1 Like

It’s the script deleting itself that’s causing the lag, but the lag is very severe and I’m not sure if its normal

1 Like

Oh that makes sense i think i have solution for you

Tag ClickDetector and use it for CollectionService and place code in ServerScriptService that don’t need external code and it won’t cause lag

1 Like

Yes that’s what I did
Is it a bug because I’m not sure why it took so long

1 Like

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