Is there a more efficient way to script this targeting Gui?

local Seeker = game.Players.LocalPlayer.PlayerGui.ScreenGui.Base.Seeker
local UIS = game:GetService("UserInputService")

local function BoundaryCheck(Gui_Instance1,Gui_Instance2)
	local Gui_Instance1_Pos, Gui_Instance1_Size = Gui_Instance1.AbsolutePosition, Gui_Instance1.AbsoluteSize;
	local Gui_Instance2_Pos, Gui_Instance2_Size = Gui_Instance2.AbsolutePosition, Gui_Instance2.AbsoluteSize;

	local Top = Gui_Instance2_Pos.Y-Gui_Instance1_Pos.Y
	local Bottom = Gui_Instance2_Pos.Y+Gui_Instance2_Size.Y-(Gui_Instance1_Pos.Y+Gui_Instance1_Size.Y)
	local Left = Gui_Instance2_Pos.X-Gui_Instance1_Pos.X
	local Right = Gui_Instance2_Pos.X+Gui_Instance2_Size.X-(Gui_Instance1_Pos.X+Gui_Instance1_Size.X)

	if Top > 0 then
		Gui_Instance1.Position = Gui_Instance1.Position + UDim2.new(0,0,0,Top)
	elseif Bottom < 0 then
		Gui_Instance1.Position = Gui_Instance1.Position + UDim2.new(0,0,0,Bottom)
	end
	if Left > 0 then
		Gui_Instance1.Position = Gui_Instance1.Position + UDim2.new(0,Left,0,0)
	elseif Right < 0 then
		Gui_Instance1.Position = Gui_Instance1.Position + UDim2.new(0,Right,0,0)
	end
end

UIS.InputBegan:Connect(function(input, gpe)
	while UIS:IsKeyDown(Enum.KeyCode.Up) do
		Seeker.Position = Seeker.Position - UDim2.new(0,0,0.01,0)
		BoundaryCheck(Seeker, Seeker.Parent)
		wait()
	end
	while UIS:IsKeyDown(Enum.KeyCode.Down) do
		Seeker.Position = Seeker.Position + UDim2.new(0,0,0.01,0)
		BoundaryCheck(Seeker, Seeker.Parent)
		wait()
	end
	while UIS:IsKeyDown(Enum.KeyCode.Left) do
		Seeker.Position = Seeker.Position - UDim2.new(0.01,0,0,0)
		BoundaryCheck(Seeker, Seeker.Parent)
		wait()
	end
	while UIS:IsKeyDown(Enum.KeyCode.Right) do
		Seeker.Position = Seeker.Position + UDim2.new(0.01,0,0,0)
		BoundaryCheck(Seeker, Seeker.Parent)
		wait()
	end
end)

Here is what this does:

Hello @MeLikeBigRifle Your code I think looks on good,

however I recommend you post this in #help-and-feedback:code-review .

1 Like

local Seeker = game.Players.LocalPlayer.PlayerGui.ScreenGui.Base.Seeker
local UIS = game:GetService("UserInputService")

local function BoundaryCheck(Gui_Instance1, Gui_Instance2)
    local Gui_Instance1_Pos, Gui_Instance1_Size = Gui_Instance1.AbsolutePosition, Gui_Instance1.AbsoluteSize
    local Gui_Instance2_Pos, Gui_Instance2_Size = Gui_Instance2.AbsolutePosition, Gui_Instance2.AbsoluteSize

    local Top = Gui_Instance2_Pos.Y - Gui_Instance1_Pos.Y
    local Bottom = Gui_Instance2_Pos.Y + Gui_Instance2_Size.Y - (Gui_Instance1_Pos.Y + Gui_Instance1_Size.Y)
    local Left = Gui_Instance2_Pos.X - Gui_Instance1_Pos.X
    local Right = Gui_Instance2_Pos.X + Gui_Instance2_Size.X - (Gui_Instance1_Pos.X + Gui_Instance1_Size.X)

    if Top > 0 then
        Gui_Instance1.Position += UDim2.new(0, 0, 0, Top)
    elseif Bottom < 0 then
        Gui_Instance1.Position += UDim2.new(0, 0, 0, Bottom)
    end
    if Left > 0 then
        Gui_Instance1.Position += UDim2.new(0, Left, 0, 0)
    elseif Right < 0 then
        Gui_Instance1.Position += UDim2.new(0, Right, 0, 0)
    end
end

local function ChangeSeekerPosition(Sign, XScale, XOffset, YScale, YOffset)
    Seeker.Position += Sign * UDim2.new(XScale, XOffset, YScale, YOffset)
    BoundaryCheck(Seeker, Seeker.Parent)
end

UIS.InputBegan:Connect(function(input)
    while UIS:IsKeyDown(Enum.KeyCode.Up) do
        ChangeSeekerPosition(-1, 0, 0, 0.01, 0)
        wait()
    end
    while UIS:IsKeyDown(Enum.KeyCode.Down) do
        ChangeSeekerPosition(1, 0, 0, 0.01, 0)
        wait()
    end
    while UIS:IsKeyDown(Enum.KeyCode.Up) do
        ChangeSeekerPosition(-1, 0.01, 0, 0, 0)
        wait()
    end
    while UIS:IsKeyDown(Enum.KeyCode.Down) do
        ChangeSeekerPosition(1, 0.01, 0, 0, 0)
        wait()
    end
end)

don’t know how much this will help but I hope this is helpful

You can replace your wait() functions with RunService.Heartbeat:Wait() (create a variable first - local RunService = game:GetService("RunService")). This can be up to 2x times faster, although I’m not sure how big of a difference it’s going to make in your case.

another option would be the new task.wait()