So I am trying to see if there is a sat function to where if someone sits on a seat, a gui would come up and .Touched() is not very accurate with it.
There are two methods that this can de done with, note they both have pros and cons.
Method one uses the changed event which means it will fire if the name is changed and other properties. Method two looks for the weld that is added, this means it will not fire except when a child is added.
-- method 1 script.Parent.Changed:Connect(function(prop) if prop == 'Occupant' then print('Occupant added or removed') else print('Ran for no reason') end end) -- method 2 script.Parent.ChildAdded:Connect(function(ins) if ins.Name == 'SeatWeld' then -- could also check for type print('Occupant weld added or removed') else print('Ran for no reason') end end)
This is just a basic example of the two methods, you will need to decide which one is better for you.
I hope this helps.
The easy way to do this is to use the .Changed
event, which fires when a property of the object changes, in conjunction with the Occupant
property. The Occupant
property is set to the Humanoid that is currently sitting on the seat. Example code that will kill whoever sits on the seat:
local seat = game.Workspace.Seat seat.Changed:Connect(function(property) if property == "Occupant" then seat.Occupant.Health = 0 end end)
If this helped, please mark this answer!
I just looked up the API for Seats (Something you should do next time if you want this type of info btw) and saw that there is this property called Occupant which appears to be new.
Occupant is a Read Only property which means you cannot edit this property in any way but you can grab it's information to view it. Occupant's value is the Humanoid of the person sitting, if there is no one sitting, then it would be nil
.
Here is a script directly from the wiki about Occupant use (I take no credit):
local seat = workspace.Car.Seat seat.Changed:connect(function(property) if property ~= 'Occupant' then return end local occupant = seat.Occupant if occupant then local character = occupant.Parent local player = game.Players:GetPlayerFromCharacter(character) if player then print(("Player %q just sat on the seat"):format(player.Name)) else print(("NPC %q just sat on the seat"):format(character.Name)) end else print("Someone left the seat") end end)
If you look over this script, all it simply does is check when the occupant property has a change, then it will check to see if the value is a humanoid and print out who (or what) sat in this seat. Assuming something got off of this seat, then it would print Someone left the seat.
If this helps, please accept this answer! Any questions/problems? Just comment bellow