How to toggle GUI when player is on/off VehicleSeat? (seat.Occupant)

Hello everyone!
I’ve been having a bit of trouble with finding a way to detect if a player is seated and not seated.
This is my code:

local repStorage = game:GetService("ReplicatedStorage")
local event = repStorage:WaitForChild("TurretGui")

local seat = script.Parent

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local hum = seat.Occupant
	if not hum then
		return 
	end

	local plr = game.Players:GetPlayerFromCharacter(hum.Parent)
	if not plr then
		return 
	end

	event:FireClient(plr, true)
end)

My goal is to show a GUI when the player sits on a VehicleSeat, but to disable the GUI when the player goes off the VehicleSeat.

The reason why I’m using “Occupant” is because there could be multiple VehicleSeats/Seats in my game.

If anyone knows the solution, please let me know.
Thanks!

What you’re doing is always sending the client a bool with the value set to true. You could try doing this.

local repStorage = game:GetService("ReplicatedStorage")
local event = repStorage:WaitForChild("TurretGui")

local seat = script.Parent

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local hum = seat.Occupant
	if not hum then
		return 
	end

	local plr = game.Players:GetPlayerFromCharacter(hum.Parent)
	if not plr then
		return 
	end

if plr.PlayerGui.YourGui.Enabled == true then
       event:FireClient(plr, false)
else
      event:FireClient(plr, true)
end
end)

Unfortunately, this script will not work because the Server can’t tell if the Client has shown the GUI. (i’m pretty sure that’s the case)

Oh yes, I forgot that lol, well in that case, just turn it on from the Server. It will only turn it on for that specific player so it should work

lol. So I used the code you used but changed it up a bit so that it doesn’t use a RemoteEvent, but however, when I go in the VehicleSeat, the GUI turns on. But if I hop off, it stays there. When I go back on the turret, it disables itself. Then it does the same thing over again etc.

There’s a script at the bottom of the link Seat | Roblox Creator Documentation that references the player in the seat. You should be able to add the GUI to that player when they sit, and remove it when they leave.

Alright, I’ll try it out. However, would this trigger the other VehicleSeats if there were any?

I had the exact issue once, wish I was in studio and I could’ve sent you the whole script. Make sure you’re turning the GUI off after the player jumped out.

Ah, ok. How would I figure that out though…

Nevermind, I found that it would only trigger the seat itself. I’ll have a look at it and change the code up.

Make a lastPlayer variable outside of the seat changed event and change that variable to the player when a player sits on it. At the start of the seat changed event add something along the lines :

lastPlayer.PlayerGui.YourGui.Enabled = false

You guys think this code will work?

local Players = game:GetService("Players")
local currentPlayer = nil
local currentGui = nil
local seat = script.Parent.VehicleSeat

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local humanoid = seat.Occupant 
	if humanoid then 
		local character = humanoid.Parent
		local player = Players:GetPlayerFromCharacter(character)
		local plrGui = player:WaitForChild("PlayerGui")
		local turretGui = plrGui:WaitForChild("TurretGui")
		if player then 
			print(player.Name.." has sat down")
			turretGui.Enabled = true
			
			currentPlayer = player
			return
		end	
	end
	if currentPlayer then 
		print(currentPlayer.Name.." has got up")
		currentGui.Enabled = false
		currentPlayer = nil
	end
end)
1 Like

Good idea. I’ll have a look into it.

1 Like

be sure to mark me as solution if I helped! thanks

If you don’t manage to do it before I wake up, I’ll send you a fully working script from my game

Alright, thank you for your help! I’ll reply back if it works or not.

1 Like

@iscriptfast it works! thanks for the help :slight_smile:
I also found another solution along with yours (but i used yours cus its tidier lol).

sorry for the late reply

1 Like