Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Destroy BillboardGui when sitting, place it back when leaving seat?

The title says it all. How do I Destroy a BillboardGui Inside a part when sitting? Workspace V Seats (folder) V Seat V GUI (BillboardGUI)

Is there a way to find all the GUIs from the seats folder and destroy them when sitting? Also, place them back after leaving a seat Notes:

  • I want to have only the player SITTING having his BillboardGUI removed LocalScript in StarterGui:
-- // Services \\ --
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

-- // Variables \\ --
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait() -- // If character isn't loaded wait till its added
local Humanoid = Character:WaitForChild("Humanoid")

local Seats = workspace.Seats -- // Seats folder where all the seats 
local RANGE = 6 -- // How close you have to be to a seat in order to press E.

-- // Functions \\ --
local function GetClosestSeat()
    local SeatsInRange = {}
    for _,seat in pairs(Seats:GetChildren()) do
        local mag = (seat.Position-Character.HumanoidRootPart.Position).Magnitude
        if mag <= RANGE and seat.Occupant == nil then
            table.insert(SeatsInRange, {Seat = seat, Mag = mag}) -- // Insert into the table because the seat is in range.
			
        end
    end

    if #SeatsInRange > 0 then -- // If there are seats near the character.
        table.sort(SeatsInRange, function(a,b) -- // Sort the table, so the closest seat is the first index of the table.
           return a.Mag < b.Mag 
        end)
        return SeatsInRange[1].Seat -- // Return the closest seat after sorting.
    else
        return nil -- // Return nil if no seats are close.
    end
end

UserInputService.InputBegan:Connect(function(Input, GameProcessed)
    if GameProcessed then return end -- // If player is typing in chat(or other gameProcessed events) then we won't run the code.

    if Input.KeyCode == Enum.KeyCode.E and Humanoid.Sit == false then -- // If E key is pressed and the humanoid is not currently sitting. 
        local ClosestSeat = GetClosestSeat()
        if ClosestSeat ~= nil then -- // If there is a close seat.
            ClosestSeat:Sit(Humanoid)
        end
    end
end)

1 answer

I'm not quite sure what the script you posted has to do with the question, but here's my approach at it. I'll walk you through what I did.

--Load external dependencies
local Players = game:GetService("Players")
	local player = Players.LocalPlayer
		local character = player.Character or player.CharacterAdded:Wait()
			local Humanoid = character:WaitForChild("Humanoid")

--The first thing we are doing is loading all the BillboardGuis, so let's use a for loop to find them all.
local guis = {}
for i,child in pairs(workspace.Seats:GetDescendants()) do
	if child:IsA("BillboardGui") then
		--We found a BillboardGui, let's save it, as well as its original parent for later
	 	guis[#guis + 1] = {
			Gui = child,
			Parent = child.Parent
		}
	end
end

--SeatPart is a property of Humanoid. Whenever the player sits or gets up, let's check what's going on
Humanoid.Changed:Connect(function(property)
	if property == "SeatPart" then
		--Player is sitting down or just stood up, let's hide or show the UI now
		for i,guiData in pairs(guis) do
			--This will set the BillboardGui's parent to its original parent if not sitting, or hide if the player is sitting
			guiData.Gui.Parent = ((not Humanoid.SeatPart) and guiData.Parent) or nil
		end
	end
end)

2 comments

0

I think this issue is being approached in the wrong manner. Why delete it instead of setting it to Visible (or Enabled) = false?

0

He asked for it to be Destroyed so I made a method more similar to that without actually destroying the GUI.

This discussion is preserved as it was. New answers, comments, and votes are closed.