Weird lag when destroying an object

Im trying to make the player’s screen flash for a moment when they touch the object shown in the video. However, for SOME reason, the game freezes for a good second after the object is destroyed. I’ve genuinely tried everything, but i still can’t figure out how to fix this. I’ve tried making the object transparent instead of outright destroying it, and that seemed to KINDA fix the issue, but the flash still came onto the screen with a delay.

Server:

local debounce = false

task.spawn(function()
	while task.wait(0.15) do
		script.Parent.CFrame *= CFrame.Angles(0, math.rad(20), 0)
	end
end)

script.Parent.Touched:Once(function(p)
	if debounce == false and p.Parent:FindFirstChild("Humanoid") then
		debounce = true
		script.Parent:Destroy()
		game.ReplicatedStorage.Flash:FireAllClients()
	end
end)

Client:

local event = game.ReplicatedStorage:WaitForChild("Flash")

event.OnClientEvent:Connect(function()
	local player = game.Players.LocalPlayer

	local vig = player.PlayerGui.Vignette
	vig.ImageLabel.ImageTransparency = 0.2
	game.TweenService:Create(vig.ImageLabel, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {ImageTransparency = 1, BackgroundTransparency = 1}):Play()
end)

I’ve personally had that experience with the game freezing when destroying an object. It happens the first time and then it won’t happen again anymore after that. It also only happens in studio, so if you do the same in an actual server everything should be fine.

3 Likes

jeez man that not how debounce works

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Flash:RemoteEvent = ReplicatedStorage.Flash
local part:BasePart = script.Parent
local rot:CFrame = CFrame.Angles(0, math.pi/9, 0)


local connection:RBXScriptConnection

connection = part.Touched:Connect(function(p:BasePart):()
	if p.Parent:FindFirstChild("Humanoid")~=nil then
		connection:Disconnect()
		part:Destroy()
		Flash:FireAllClients()
	end
end)

while true do
	part.CFrame *= rot
	task.wait(0.15)
end

yea ive been having the same issue with the game freezing but is there anyway i can stop studio lagging after the first destroyed instance?

That may very well be your device if you create and destroy instances in studio a lot, normally the lag should stop quickly after the first Destroy() is called

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