Seating Script Issue

I’m trying to make a script that will, when a player joins, make them sit on a random seat from a table. It will remove and add seats depending on whether they’re occupied or not.

The problem is that when I play, nothing happens. No errors, problems, nothing.

Here’s the code I’m using:

-- List of parts
local PartsList = {
	game.Workspace.Seat1, 
	game.Workspace.Seat2, 
	game.Workspace.Seat3, 
	game.Workspace.Seat4,
	game.Workspace.Seat5,
	game.Workspace.Seat6
}

-- When a Player joins the game, move them to a random seat from the list
game.Players.PlayerAdded:Connect(function(player) 
	-- Find a seat that isn't occupied
	local foundSeat = false
	while not foundSeat do
		local randomSeat = PartsList[math.random(1, #PartsList)] 
		if randomSeat.Occupant == nil then 
			foundSeat = true
			player.CharacterAdded:Wait()
			player.Character.HumanoidRootPart.CFrame = randomSeat.CFrame
			-- If the seat becomes occupied, remove it from the array
			randomSeat:GetPropertyChangedSignal("Occupant"):Connect(function(newOccupant)
				if newOccupant ~= nil then
					table.remove(PartsList, table.find(PartsList, randomSeat))
				else 
					-- When vacancy is created, add it back
					table.insert(PartsList, randomSeat)
				end 
			end)
		end
	end
end)

Like I said, no errors are being output, and I’m not sure why.
I would appreciate any advice on how to fix this.

1 Like
-- List of parts
local PartsList = {
	game.Workspace.Seat1, 
	game.Workspace.Seat2, 
	game.Workspace.Seat3, 
	game.Workspace.Seat4,
	game.Workspace.Seat5,
	game.Workspace.Seat6
}

-- When a Player joins the game, move them to a random seat from the list
game.Players.PlayerAdded:Connect(function(player) 
	-- Find a seat that isn't occupied
	local foundSeat = false
	while not foundSeat do
		local randomSeat = PartsList[math.random(1, #PartsList)] 
		if randomSeat.Occupant == nil then 
			foundSeat = true
			player.CharacterAdded:Wait()
			local character = player.Character
			if character then
				local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
				if humanoidRootPart then
					humanoidRootPart.CFrame = randomSeat.CFrame
					-- If the seat becomes occupied, remove it from the array
					randomSeat:GetPropertyChangedSignal("Occupant"):Connect(function(newOccupant)
						if newOccupant ~= nil then
							table.remove(PartsList, table.find(PartsList, randomSeat))
						else 
							-- When vacancy is created, add it back
							table.insert(PartsList, randomSeat)
						end 
					end)
				end
			end
		end
	end
end)

I didn’t really understand what you want to do with your script but I tried to correct it, if I understood correctly

local SeatParts = {
	game.Workspace.Seat1, 
	game.Workspace.Seat2, 
	game.Workspace.Seat3, 
	game.Workspace.Seat4,
	game.Workspace.Seat5,
	game.Workspace.Seat6
}

game:GetService("Players").PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Wait()
	
	local Hum = Player.Character:WaitForChild("Humanoid")
	local RandomSeat
	task.wait(1) --// Wait so everything loads and stuff
	
	repeat 
		RandomSeat = SeatParts[math.random(1, #SeatParts)]
		if (RandomSeat.Occupant == nil) then 
			RandomSeat:Sit(Hum)
		end
		task.wait()
	until (Hum.SeatPart ~= nil)
	
	table.remove(SeatParts, table.find(SeatParts, RandomSeat))
	
	RandomSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
		if (RandomSeat.Occupant == nil) then 
			table.insert(SeatParts, RandomSeat)
		end
	end)
	
	task.spawn(function()
		while true and task.wait(1) do 
			print(SeatParts)
		end
	end)
end)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.