Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a sat function for a seat?

Asked by 8 years ago

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.

0
We had a competition on answering your question (You should like mine so I win). Totally accept and upvote fairly. (pick me) alphawolvess 1784 — 8y
0
No, pick me. I was first! ProgrammerA10 85 — 8y
0
All your guy's answers were great and I thumbed them all up but I went with kingdom5's script but all of your scripts worked. BennyBoiOriginal 293 — 8y
0
You mean an event, not a function. Link150 1355 — 8y

3 answers

Log in to vote
2
Answered by 8 years ago

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.

1
noob alphawolvess 1784 — 8y
0
Someone's feeling salty. Link150 1355 — 8y
Ad
Log in to vote
2
Answered by 8 years ago
Edited 8 years ago

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!

1
Save the occupant in a variable, and when Occupant changes to nil, check the save variable to find out who to take the GUI away from. ProgrammerA10 85 — 8y
Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

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

Answer this question