When seat is occupied unanchor all parts in the chair model | Scripting help

Hello Developers!

I am fairly new to scripting and I wanted to try something. I tried to make a Chair that would unanchor all the parts including the seat when it would be sat on. So far I haven’t been able to make it work, I get no errors in my output. Here is my code:

local model = workspace.Model
local seat = workspace.Model.ChairSeat


for _, part in ipairs(model:GetDescendants()) do
	if seat.Occupant == true then
		wait(3)
		if part:IsA("Part") then
			part.Anchored = false
		end
end	
end

Thank you so much!

-Skull

You need an event/signal first, I recommend using :GetPropertyChangedSignal(). You need it because script will fire instantly and it won’t do anything because seats are unoccupied by default.

local model = workspace.Model
local seat = workspace.Model.ChairSeat

seat:GetPropertyChangedSignal('Occupant'):Connect(function() -- This will fire whenever the seat's Occupant property is changed
    if seat.Occupant then -- Don't check if the seat.Occupant property is true, check that it isn't nil instead
        for i,part in pairs(model:GetDescendants()) do -- Only go through the loop if the seat has an occupant
            if part:IsA('BasePart') then -- Check if it's a BasePart in case it's a union or a mesh unless you don't want to check for any unions or meshes
                part.Anchored = false
            end
        end
    end
end
1 Like

Thank you so much! I knew I should of thought about using GetPropertyChangedSignal!

1 Like