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

How to make player invisible when he sits on seat??

Asked by 2 years ago
Edited 2 years ago

Hi is it possible to make the player invisible when he sits on a seat, then get uninvisible when he gets off it?? How would I be able to do this --Thanks for any help

1 answer

Log in to vote
0
Answered by
imKirda 4491 Moderation Voter Community Moderator
2 years ago
Edited 2 years ago

the first thing you need to make is a function that will change character's transparency to your specified value, you loop through every descendant of the character (children, their children and so on) and check if that descendant is a part, if that's so then change its transparency.

local function Set_Character_Transparency(character, transparency)
    for _, part in character:GetDescendants() do
        if part:IsA("BasePart") and part.Name ~= "HumanoidRootPart" then
            part.Transparency = transparency
        end
    end
end

next you need to make player invisible when he sits down, say you have a seat:

local seat = workspace.Seat

then you want to know when someone sits on this seat, you can use Seat.Occupant, this property shows who is currently sitting on the seat, if no-one is sitting then it's nil, otherwise it's Humanoid of the person who's sitting.

everytime this property changes you want to make the sitting character invisible, you just run the Set_Character_Transparency function with transparency being 1 (which is invisible). to run a function when the property changes, you want to use :GetPropertyChangedSignal which will do exactly that:

local seat = workspace.Seat

local function Set_Character_Transparency(character, transparency)
    for _, part in character:GetDescendants() do
        -- never change transparency of humanoid root part, it's always invisible
        if part:IsA("BasePart") and part.Name ~= "HumanoidRootPart" then
            part.Transparency = transparency
        end
    end
end

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
    print("Occupant changed:", seat.Occupant)

    -- seat.Occupant may be nil if no one is sitting, that's why there's if
    if seat.Occupant then
        -- seat.Occupant.Parent is the character (Humanoid is in Character makes sense)
        Set_Character_Transparency(seat.Occupant.Parent, 1)
    end
end)

but now you want to make character visible back again, for that you need to remember the humanoid who last sit on the seat, and when the seat occupant changes again you check: was there anyone sitting before? if there was someone sitting before and the seat occupant changed then it means that currently there is no-one sitting, so we make the last sitting character back visible.

local seat = Workspace.Seat

-- last seating character
local LastOccupant = nil

local function Set_Character_Transparency(character, transparency)
    for _, part in character:GetDescendants() do
        if part:IsA("BasePart") and part.Name ~= "HumanoidRootPart" then
            part.Transparency = transparency
        end
    end
end

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
    local occupant = seat.Occupant


    if occupant == nil and LastOccupant then
        -- if occupant is nil (no one is seating) and someone was sitting before, we make
        -- him back visible
        Set_Character_Transparency(LastOccupant, 0)
    else
        -- otherwise if occupant is not nil then someone just seated so we set him
        -- as the last seating occupant and make him invisible
        Set_Character_Transparency(occupant.Parent, 1)
        LastOccupant = occupant.Parent
    end
end)

https://developer.roblox.com/en-us/api-reference/function/Instance/GetPropertyChangedSignal

0
say if you need more explanation i am not sure what's your "skill" level imKirda 4491 — 2y
0
Hi i will need more of an explanation, also where am I going to put all these scripts? All in Seat? Btw I have little to no skill with Lua coding still learning. theking66hayday 841 — 2y
0
this explains the functionaliy of roblox more rather than Luau so if you don't know Luau you won't understand probably imKirda 4491 — 2y
Ad

Answer this question