Seat occupant becomes nil

So I created this seat occupant script and there is a problem with it. It basically will not execute the line of code (commented) because occupant is reset to nil. When a occupant leaves the seat, the occupant becomes nil, but how do you still retain the value of the previous occupant?

tool.Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local occupant = tool.Seat.Occupant.Parent
	
	if tool.Seat.Occupant then
		for i, j in pairs(occupant:GetChildren()) do
			if j.ClassName == "Part" then
				j.Massless = true
				print("Ok massless")
			end
		end
	else --this is the problem because the statement alredy said that occupant = nil
		for i, j in pairs(occupant:GetChildren()) do --attempt to index nil, how would i retain the value of the previous occupant?
			if j.ClassName == "Part" then
				j.Massless = false
				print("This DOES NOT PRINT")
			end
		end
	end
end)

If you’re trying to retain the value of the previous occupant, then set a variable.
What are you trying to do? When the person sits down he becomes massless?

Right now the person sitting down causes mass bc all of the player’s joints have mass, it is causing some issues with my vehicle thing.

Actually, I was about to reply with setting a variable, but I noticed you already did that, so I’m going to test it on RS myself

Oh, I see. It’s because you’re redefining the variable every time the seat occupant leaves or enters. Meaning that when the seat occupant leaves, the variable occupant’s value will be set to nil.

But the reason why I did that is because I don’t want the value to retain forever, because a new person might sit in the seat and it still thinks the old person is massless.

You can set occupant to nil after it is changed. Just put
local occupant before the GetPropertyChangedSignal
I’m also testing in RS

So where do I put occupant == nil?

I found it. Here’s what I did.

local seat = script.Parent
local occupant
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if seat.Occupant then
		occupant = seat.Occupant.Parent
	else
		
		print(occupant) --it prints!
	end
end)

You could probably make occupant nil after they leave.

1 Like

What does it print? fdaaaaaaaaaaaaaaaaaaaaaa

Is your seat thing working now? (Also it prints the occupant’s name)

It prints the character name seat.Occupant.Parent
seat = seat
seat.Occupant = Humanoid
seat.Occupant.Parent = Character