Trying to sit players in a random seat

hello! i want players to sit in a random seat but i keep getting this error. idk what im doing tbh. ty!

problem:

					local randomSeat = seat[math.random(1, #seatFolder)]

script:

	game.Players.PlayerAdded:Connect(function(p)
		p.CharacterAdded:Connect(function(c)
			local seatFolder = workspace.tableModel.seatFolder

			local hum:Humanoid = c.Humanoid

			for seatName, seat:Seat in ipairs(seatFolder:GetChildren()) do

				
				if seat:IsA("Seat") then

					local randomSeat = seat[math.random(1, #seatFolder)]

					if randomSeat:GetAttribute("occupied") == false then
						c:PivotTo(randomSeat.CFrame* seatOFFSET) 
						wait(.5)
						randomSeat:Sit(hum)
						if randomSeat.Occupant then
							randomSeat:SetAttribute("occupied", true)
							randomSeat:SetAttribute("playerName", randomSeat.Occupant.Parent.Name)
						end
						break
					end
				end				
			end
			
		end)
	end)

image

Just looking at one part of this script … I don’t think you can use true or false with Occupant.

testing …

local seat = workspace:WaitForChild("Seat")
if seat.Occupant == nil then
	print("x")
end

But that works.

1 Like

hey man, thanks for ur response. basically my “occupied” thing is an attribute right and ive got this table with a load of seats. i just want it so that i wont be sitting in the same place every time. is there a way to do that?

i spawn in this same chair everytime i join:

Basically seat.Occupant can have 2 values. A nil value where nothing is sat in the seat. Or a value of displaying what is sat in the seat. In your case a player the value of seat.Occupant would be humanoid.

So instead of if randomSeat:GetAttrivute(“occupied”) == false then …
do

local humanoid = randomSeat.Occupant
if humanoid then...

When there is no player in the seat, humanoid will be false and therefor the if statement will be false.

1 Like

hey klm,
thank u 4 ur response. i figured it out eventually with a bit of help from this post so credits to him but tysm 4 ur help: :slight_smile:

solution:

	local seatOFFSET=CFrame.new(0,2,0)

	game.Players.PlayerAdded:Connect(function(p)
		p.CharacterAdded:Connect(function(c)
			local seatFolder = workspace.tableModel.seatFolder
			local hum:Humanoid = c.Humanoid
			local randomSeat = seatFolder:GetChildren()[math.random(1, #seatFolder:GetChildren())]

			if randomSeat:GetAttribute("occupied") == false then
				c:PivotTo(randomSeat.CFrame* seatOFFSET) 
				wait()
				randomSeat:Sit(hum)
				if randomSeat.Occupant then
					randomSeat:SetAttribute("occupied", true)
					randomSeat:SetAttribute("playerName", randomSeat.Occupant.Parent.Name)
				end			
			end	
		end)
	end)
end

You don’t have to manually create a table of all the instances inside of the folder. That’s what :GetChildren() is for.

local randomSeat = seatFolder:GetChildren()[math.random(1, #seatFolder:GetChildren())]
1 Like

rumble, again?!!?!? if i got a penny for everytime i saw u on devforum i would be rich rn. :sob:

incredible info man. i tried something close to that but i couldnt connect it up. tysm!