Hi everyone, I was wondering if you could help me with this issue. What I need to do is give a player a gui when they sit in a seat, however I can’t figure out how to detect when they’ve sat and what do do from there. Any ideas and help is appreciated!
seat:GetPropertyChangedSignal(“Occupant”)
Use that function to detect a humanoid change then use the GetPlayerFromCharacter function from players to get the character.
How do I get the character/player?
local Players = game:GetService("Players")
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
local char = seat.Occupant.Parent
local player = Players:GetPlayerFromCharacter(char)
end)
Seat.Occupant is the Humanoid of the player currently seated.
What coould I do for the player getting out of the seat to destroy the GUI?
local Players = game:GetService("Players")
local lastPlayer -- upvalue
local seat
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
if not seat.Occupant and lastPlayer then
local PGui = lastPlayer.PlayerGui
PGui.Gui:Destroy()
end
local char = seat.Occupant.Parent
local player = Players:GetPlayerFromCharacter(char)
lastPlayer = player
end)
If seat.Occupant is nil then there’s no one seated, destroy the Gui from the last player who sat.
Edit:
but two people can’t sit on the same seat at the same time?
Each time a player either sits or leaves, that’ll run individually.
Or you could do this through a local script to make things easier too.
What about if multiple people sit in the seat?
Through private discussion I got your current script, and fixed it here:
local seat = script.Parent
local guiName = "DoorControl318"
local gui = script:WaitForChild(guiName)
local Players = game:GetService("Players")
local lastplr
-- each time the Occupant property of the seat fires...
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
if seat.Occupant ~= nil then
local char = seat.Occupant.Parent
local player = Players:GetPlayerFromCharacter(char)
local cloned = gui:Clone()
-- cloned.TrainName.Value = script.Parent.Parent.Parent.Name
cloned.Parent = player.PlayerGui
lastplr = {player, cloned} -- store player and Gui in array
else
print("player left")
if lastplr then -- verify lastplayer is not nil
lastplr[2]:Destroy() -- since the table's numerically indexed, we know lastplr[2] will be the Gui
lastplr = nil
end
end
end)