Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Seat Occupant Help Needed?

It does both at the same time so it sets Transparency to 1 then 0

local seat = game.Workspace.GreenCarSeat
local occupant

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	
	if seat.Occupant ~= nil then
		
		occupant = seat.Occupant.Parent
		
		local PlayerName = seat.Occupant.Parent
		
		local descendats = game.Workspace[PlayerName.Name]
		
		for _, v in pairs(descendats:GetDescendants()) do
			if ((v:IsA("BasePart") or v:IsA("Decal")) and v.Name ~= "HumanoidRootPart" and "Accessory") then
				v.Transparency = 1
		
	else
				
				for _, v in pairs(descendats:GetDescendants()) do
					if ((v:IsA("BasePart") or v:IsA("Decal")) and v.Name ~= "HumanoidRootPart" and "Accessory") then
						v.Transparency = 0
				
						
					end	
				end		
			end			
		end				
	end
end)

1 answer

✓ Accepted answer

You put else statement for

if ((v:IsA("BasePart") or v:IsA("Decal")) and v.Name ~= "HumanoidRootPart" and "Accessory") then

This means that it will set player's transparency to 0 if this single part is base part or decal and is not root part, there was nearly 100% for this to happen.

From what I understood you want the player to be invisible when sitting on the seat, after changing your code a little I made the following:

local seat = game.Workspace.GreenCarSeat

local PreviousOccupant = nil

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
  -- If there was an occupant previously and it has changed then logically
  -- the occupant left the seat, this means we should make him visible
  -- and not continue the function again
  if PreviousOccupant then
    for _, v in pairs(PreviousOccupant:GetDescendants()) do
      if ((v:IsA("BasePart") or v:IsA("Decal")) and v.Name ~= "HumanoidRootPart" and "Accessory") then
        v.Transparency = 0
      end
    end
    
    PreviousOccupant = nil
    return
  end
  
  -- Stop the function is there is no new occupant
  if seat.Occupant == nil then
    return
  end
  
  -- Make the new occupant visible
  local occupant = seat.Occupant.Parent

  for _, v in pairs(occupant:GetDescendants()) do
    if ((v:IsA("BasePart") or v:IsA("Decal")) and v.Name ~= "HumanoidRootPart" and "Accessory") then
      v.Transparency = 1
    end
  end

  PreviousOccupant = occupant
end)

One thing you should note is that

if v.Name ~= "HumanoidRootPart" and "Accessory" then

is equal to

if v.Name ~= "HumanoidRootPart" then

in human words, you can read it as "if v's name is not humanoid root part and string Accessory is not nil then ...". I don't know what you wanted to achieve with that statement so I did not change it, possibly you meant something out of these:

if ((v:IsA("BasePart") or v:IsA("Decal")) and v.Name ~= "HumanoidRootPart" and v:IsA("Accessory")) then

or

if ((v:IsA("BasePart") or v:IsA("Decal")) and v.Name ~= "HumanoidRootPart" and v.Name ~= "Accessory") then

This discussion is preserved as it was. New answers, comments, and votes are closed.