So I need it so that when someone sits in a seat it will run some script (a UI will come up). I have no idea on how I could make this though and have looked for quite a while trying to find out how. I was wondering if it could be done via the seat itself but could not find anything.
I believe the seat class has a property named Occupant, you can connect a GetPropertyChangedSignal to that and it will fire whenever a player gets in or out of the seat.
Since the occupant property holds the player sitting in it, that is how you can get the player and therefore access to their playergui. If the value is nil, dont try to give anyone a ui as nobody is sitting in it.
Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
if Seat.Occupant then
Ui:Clone().Parent = game.Players:GetPlayerFromCharacter(Seat.Occpuant.Parent).PlayerGui
end
end)
Very basic (I’m on mobile), but you should get the idea
Forgot occupant is a humanoid, not player instance!
As @Blokhampster34 said above, you can use GetPropertyChangedSignal(“Occupant”) on the seat to create an event for someone sitting or getting up. When this event is fired, you can check if the occupant is nil (nobody is sitting) or if it is a humanoid (the players humanoid who is sitting on it)
You can also use Instance.DescendantAdded and Instance.DescendantRemoving events on the seat because a weld will be created and parented to the seat when someone sits in it. The weld is destroyed when the player exits the seat.
Example code:
local seat = workspace.Seat --// Change this if needed.
seat.DescendantAdded:Connect(function(p)
if p.ClassName == "Weld" then
print("a") --// Replace this with whatever you want to happen when someone sits in the seat.
end
end)
seat.DescendantRemoving:Connect(function(r)
if r.ClassName == "Weld" then
print("b") --// Replace this with whatever you want to happen when someone gets out of the seat.
end
end)
Yes, I forgot all about the weld. Personally I would use the Occupant property combined with PropertyChangedSignal as it only requires one event but I guess it’s up to preference!
You would check if someone is sitting on the seat, if there’s no one you’d go to the Player’s PlayerGui and remove the UI
would be something like this:
(Using @Blokhampster34 example script)
local player
Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
if Seat.Occupant then
player = game.Players:GetPlayerFromCharacter(Seat.Occupant.Parent)
Ui:Clone().Parent = player.PlayerGui
else
player.PlayerGui.UILocation:Destroy() --Put UI Name here
end)