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

When making a script how would you know when a player is sitting? *specific title*

Asked by 4 years ago

Example: Imagine you wanted to make a seat that gives 1 leader stat per second while you sit on it

How would the script detect if the player is sitting on it?

2 answers

Log in to vote
0
Answered by
Sparks 534 Moderation Voter
4 years ago
Edited 4 years ago

I am making a lot of assumptions here due to the vague details, but something you can use is GetPropertyChangedSignal with the Seat.Occupant property

Assuming there is already a leaderstat folder and a "Seconds" IntValue inside of it:

-- Script should be inside the seat
local Seat = script.Parent
local Players = game:GetService("Players")

Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
    --Get seat occupant
    local humanoid = Seat.Occupant

    --Check to make sure seat is not empty
    if humanoid == nil then return end

    --Get the player sitting in the seat
    local occupant = Players:GetPlayerFromCharacter(humanoid.Parent)

    --Get that player's seconds from the leaderboard
    local seconds = occupant.leaderstats.Seconds    

    --Increment their seconds on the leaderboard by 1 while sitting
    while Seat.Occupant == humanoid do wait(1)
        Seconds.Value += 1
    end
end)

0
Sorry for the lack of detail, but I only wanted the seat.occupant but thanks! I dont know which answer to mark as answer Script_Summary 13 — 4y
0
It wont let me answer .-. Script_Summary 13 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Well, this script may help you:

while true do
    if script.Parent.Occupant ~= nil then
        script.Parent.Color = Color3.fromRGB(255, 0, 0)
    elseif script.Parent.Occupant == nil then
        script.Parent.Color = Color3.fromRGB(27, 42, 53)
    end
    wait()
end

Basically what this script does, if you add it to a Seat, when the seat is being used by someone the seat becomes red, and when it's not, it becomes black (assuming the original color of the seat is black). The code-lines 3 and 5 can be replaced with anything actually, so yeah, I guess there you have it.

Answer this question