Disabling a certain game UI when seated

Hey everyone, there is some UI in my game that obstructs the view of players when they’re driving a vehicle. Therefore, I am trying to construct a script that disables the UI (this UI is found in StarterGui and is a ScreenGui)

however, it does nothing.

My code:


local seat = workspace.Seat
local gui = game.Players.LocalPlayer.PlayerGui.UI

-- Function to disable the parent GUI when player is seated
local function disableParentGuiWhenSeated()
	if seat.Occupant then
		gui.Enabled = false
	else
		gui.Enabled = true
	end
end


disableParentGuiWhenSeated()


seat.OccupantChanged:Connect(disableParentGuiWhenSeated)
1 Like

Have you tried instead using the Humanoid.StateChanged and check if it’s Seated?

You can instead just do this:

function OnSeatChange()
    local boolean = (seat.Occupant ~= nil)
    gui.Enabled = boolean
    -- Alternative:
    gui.Enabled = (seat.Occupant ~= nil)
end

If you want to reverse it, you can change ~= to ==:

function OnSeatChange()
    gui.Enabled = (seat.Occupant == nil)
    -- Alternative:
    gui.Enabled = (not seat.Occupant)
end

I think it’s better if this script is placed inside the UI itself. Meaning that script.Parent will be the gui variable also ensure it’s a LocalScript. Using design techniques like this often minimizes some annoying errors related to replication, player resetting, UI loading, and such.

After changing it, it still has not worked.