How do I make a character transparent when seated and back to normal when not?

I need to make it so when a player seats on a “Seat” it becomes transparent and when he gets out of the seat he will be visible as he was before!

I got it working when it’s seated (he became transparent), but when he leaves I don’t know how to make him transparent again?
I’ve tried many posiblities but nothing worked.

Code:

local part = script.Parent
local char

while wait(0.2) do
	if part.Occupant then
		char = part.Occupant.Parent
		for i,v in pairs(char:GetDescendants()) do
			if v:IsA("Part") or v:IsA("MeshPart") or v:IsA("Decal") then
				v.Transparency = 1
				print("doing")
				if not part.Occupant then
					break
				end
			end
		end
	else
		print("no seat")
	end
end

I personally think your should take another aproach.
See : Seat | Roblox Creator Documentation

Code from the roblox docs

local Players = game:GetService("Players")

local seat = Instance.new("Seat")
seat.Anchored = true
seat.Position = Vector3.new(0, 1, 0)
seat.Parent = workspace

local currentPlayer = nil

local function onOccupantChanged()
	local humanoid = seat.Occupant
	if humanoid then
		local character = humanoid.Parent
		local player = Players:GetPlayerFromCharacter(character)
		if player then
			print(player.Name .. " has sat down")
			currentPlayer = player
			return
		end
	end
	if currentPlayer then
		print(currentPlayer.Name .. " has got up")
		currentPlayer = nil
	end
end

seat:GetPropertyChangedSignal("Occupant"):Connect(onOccupantChanged)

This code is better for many reasons, first of all it is not a while loop, and secondly you don’t have to loop over char:GetDescendants(). I still recommend you to read the roblox documentation (the link above) to have a better understanding of what this script does.

From there, you can modify the script to hide and display whatever you want on the seat!

1 Like

Thank you so much, I’ve read the document and I understood how it works, you were a big help, have a nice day!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.