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?
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.
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.