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)
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.
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.
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 :
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)