BillboardGui is not created if there is an existing UI and you are moving to a closer one

Hello. I made an E interaction to grab some items, yet, if there is an existing UI and you move to a closer existing one, it won’t create a new BillboardGui. I am really bad at explaining my issue, here is a video and my script:
https://streamable.com/js4310
My script:

local billbaord = script.Parent;

local userInputService = game:GetService("UserInputService");

local lastUI = nil;

local lastInteractable = nil;

local function input_Began(input, isTyping)
	if isTyping then return end;
	
	if input.KeyCode ~= Enum.KeyCode.E then return end;
	
	local tweenService = game:GetService("TweenService");
	
	local tweenInfo = TweenInfo.new(0.3);
	
	local goal = {
		Size = UDim2.new(0.428, 0,0.866, 0);
		BackgroundColor3 = Color3.fromRGB(41, 82, 124)
	};
	
	tweenService:Create(lastUI.Around.Inside, tweenInfo, {
		BackgroundColor3 = Color3.fromRGB(41, 82, 124)
	}):Play()
	
	local track = tweenService:Create(lastUI.Around, tweenInfo, goal);
	
	track:Play()
	
	lastUI.Around.Inside.InsideBar.Size = UDim2.new(0.34, 0,0.363, 0)
	
	lastUI.Around.Inside.InsideBar.Visible = true
	
	local track2 = tweenService:Create(lastUI.Around.Inside.InsideBar, TweenInfo.new(1), {
		Size = UDim2.new(1, 0, 1, 0)
	})
	
	track2:Play()
	
	userInputService.InputEnded:Connect(function(input, isTyping)
		if isTyping then return end;
		
		if input.KeyCode == Enum.KeyCode.E then
			tweenService:Create(lastUI.Around, tweenInfo, {
				Size = UDim2.new(0.528, 0,1.006, 0);
				BackgroundColor3 = Color3.fromRGB(85, 170, 255)
			}):Play()
			
			tweenService:Create(lastUI.Around.Inside, tweenInfo, {
				BackgroundColor3 = Color3.fromRGB(85, 170, 255)
			}):Play()
			
			lastUI.Around.Inside.InsideBar.Visible = false
			
			track2:Stop()
		else
		end
	end)
end;

userInputService.InputBegan:Connect(input_Began)

while wait() do		
	local closestPoint = 7;

	local interactables = workspace.Interactables:GetChildren();
	
	if lastInteractable and (game.Players.LocalPlayer.Character.UpperTorso.Position - lastInteractable.Position).Magnitude > closestPoint then lastUI:Destroy() else end
	
	for _, v in ipairs(interactables) do
		if (game.Players.LocalPlayer.Character.UpperTorso.Position - v.Position).Magnitude < closestPoint then
			lastInteractable = v
			
			local clone = script.Interaction:Clone();
			
			clone.Parent = v;
			
			lastUI = clone
			
			repeat wait()
				
			until (game.Players.LocalPlayer.Character.UpperTorso.Position - v.Position).Magnitude > closestPoint
			
			lastUI:Destroy()
		else
		end
	end
end

Does this work? Instead of just checking if the interactables are close enough, it now also checks which one is the closest one. I also changed the distance check to use squared distance because I believe it might be more efficient without the squareroot.

local maxInteractDist = 7
local maxDistP2 = maxInteractDist^2
local plr = game.Players.LocalPlayer
local lastInteractable, lastUI
while wait() do
	local char = plr.Character
	local hrp = char and char:FindFirstChild("HumanoidRootPart")
	if hrp then
		local smallestDist
		local newInteractable
		local hrpPos = hrp.Position
		for _, v in ipairs(interactables) do
			local posDiff = hrpPos-v.Position
			local distP2 = posDiff.X^2+posDiff.Y^2+posDiff.Z^2
			if distP2 < maxDistP2 and (not smallestDistP2 or distP2 < smallestDistP2) then
				newInteractable = v
				smallestDistP2 = distP2
			end
		end
		if lastInteractable ~= 	newInteractAble then
			if lastUI then
				lastUI:Destroy()
			end
			if newInteractable then
				local clone = script.Interaction:Clone();
				clone.Parent = newInteractable
				lastUI = clone
			else lastUI = nil
			end
			lastInteractable = newInteractable
		end
	end
end
1 Like