Help to optimize instance destroying. Code overall optimization

Hello developers!

I am having some trouble with not braining on how to decrease delay for destroying instance from object and table, script was quick made so any suggestions acceptable. Thank you.

local plr = game.Players.LocalPlayer
wait(2)
local char = plr.Character
local camera = workspace.CurrentCamera
local SourceLight = workspace:WaitForChild("HeadLight")
local RunService = game:GetService("RunService")
local mouse = game.Players.LocalPlayer:GetMouse()
local TweenService = game:GetService("TweenService")

local allHighlights = {}

local magnitudeFunction = game.ReplicatedStorage.Events.Magnitude

local previousObject

local function HighlightInteraction(object)
	local highlight = Instance.new("Highlight")
	local tweenIn = TweenService:Create(highlight, TweenInfo.new(0.5), {OutlineTransparency = 0.5})
	local tweenOut = TweenService:Create(highlight, TweenInfo.new(0.5), {OutlineTransparency = 0})
	highlight.FillTransparency = 1
	highlight.OutlineColor = Color3.new(1, 1, 1)
	highlight.Parent = object
	local inTable = table.insert(allHighlights, highlight)
	local tweenTransparency = coroutine.wrap(function()
		repeat --Any better alternative?
			tweenIn:Play()
			tweenIn.Completed:Wait()
			tweenOut:Play()
			tweenOut.Completed:Wait()
		until not highlight
		warn("Completed tweenTrasnparency")
	end)
	tweenTransparency()
end

RunService.RenderStepped:Connect(function()
	if SourceLight.SpotLight.Enabled == true then
		local tween = TweenService:Create(SourceLight, TweenInfo.new(0.1), {CFrame = camera.CFrame})
		tween:Play()
	end
	local object = mouse.Target
	if object and previousObject ~= object then
		if object:FindFirstChild("CanBeOpen") then
			if plr:DistanceFromCharacter(object.Position) <= 8 then
				mouse.Icon = "rbxassetid://16722134267"
				HighlightInteraction(object)
			elseif plr:DistanceFromCharacter(object.Position) >= 8 then
				mouse.Icon = "rbxassetid://16722231082"
			end
		elseif object:FindFirstChildOfClass("ClickDetector") then
			local cd = object:FindFirstChildOfClass("ClickDetector")
			if plr:DistanceFromCharacter(object.Position) <= cd.MaxActivationDistance then
				mouse.Icon = "rbxassetid://16722134267"
				HighlightInteraction(object)
			elseif plr:DistanceFromCharacter(object.Position) >= cd.MaxActivationDistance then
				mouse.Icon = "rbxassetid://16722231082"
			end
		else
			mouse.Icon = "rbxassetid://16722231082"
			if #allHighlights > 0 then --Problem--
				for i,v in ipairs(allHighlights) do
					v:Destroy()
					table.remove(allHighlights, i)
				end
				print("Active highlights:", #allHighlights)
			end
		end
	end
	previousObject = object
end)

You could use connections here:

tweenIn.Completed:Connect(function()
    if not highlight then
        warn("Completed tweenTrasnparency")
        return 
    end
    tweenOut:Play()
end)

tweenOut.Completed:Connect(function()
    if not highlight then
        warn("Completed tweenTrasnparency")
        return
    end
    tweenIn:Play()
end)

tweenIn:Play()

As for your overall code, it depends on what your thing does. Ideally, you’d reuse the same highlight instead of destroying and creating new ones, though if you’re highlighting multiple things at once that might get tedious.

So, for example, just move the selection highlight between different parts, and if no part should be highlighted, parent it to nil and set the adornee to nil.

The way to make deleting instances faster is to not delete them in the first place (by reusing the highlights you’ve already made). If you really need to delete them faster, especially if a lot of highlights build up the the table, you could use the Debris service, which would spread destroying the highlights across multiple frames, instead of doing it all at once.

1 Like

Thank you, my brain was in fog, xd. I’ll fix the code tomorrow’s morning. :ok_hand:

1 Like

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