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)

2 comments
I think this issue is being approached in the wrong manner. Why delete it instead of setting it to Visible (or Enabled) = false?
He asked for it to be Destroyed so I made a method more similar to that without actually destroying the GUI.