How can I optimize this code?

I’m looking to improve optimization in this script (if possible). Any tips on optimizing this script (besides variable names) will help me. I’m trying to optimize my experience as much as possible!

Code:

local interactionPositions = {
	[true] = UDim2.new(0.5, 0,0.88, 0),
	[false] = UDim2.new(0.5, 0,0.94, 3),
}

RunService:BindToRenderStep("Checker", Enum.RenderPriority.Last.Value, function()
	local target = Mouse.Target
	local dbValid = (isDB~=nil)
	InteractionNameLabel.Position = interactionPositions[dbValid]
	
	if dbValid then
		InteractionNameLabel.Text = isDB
		InteractionNameLabel.Visible = true
		return
	elseif target then
		local isInteract = target:FindFirstChild("InteractSettings")
		if (isInteract) then
			InteractionNameLabel.Text = isInteract.InteractName.Value
			InteractionNameLabel.Visible = true
			return
		end
	end

	InteractionNameLabel.Visible = false;
end)
local interactionPositions = {
    [true] = UDim2.new(0.5, 0, 0.88, 0),
    [false] = UDim2.new(0.5, 0, 0.94, 3)
}

RunService:BindToRenderStep("Checker", Enum.RenderPriority.Last.Value, function()
    local target = Mouse.Target or Mouse
    local isInteract = target:FindFirstChild("InteractSettings")

    InteractionNameLabel.Position = interactionPositions[isDB ~= nil]

    if isDB or target and isInteract then
        InteractionNameLabel.Text = isDB and isDB or isInteract.InteractName.Value
        InteractionNameLabel.Visible = true

        return
    end

    InteractionNameLabel.Visible = false
end)

this should work, I shortened the code and made it easy to read

you should use full words in variable names though

1 Like

The issue is that if the target is nil, it will throw an error. “index nil with findfirstchild()”


so I added

if Target then
	isInteract = target:FindFirstChild("InteractSettings")
end

This can’t be optimized, correct?


But also I would like to add that it doesn’t seem to be working correctly

I edited it, does the error still occur?

Nope, there’s no errors. It’s just the issue that it doesn’t work beause I didn’t provide the full context.

what doesn’t work with the code?

Actually I was wrong about your solution not working. Instead of if Target I had if isInteract when the variable "isInteract" is being assigned to. So I will give you the solution for this! Thank you for helping, and sorry for that slight error of mine! :blue_heart: