Distribute players equally to wooden chairs

  1. What do you want to achieve? Keep it simple and clear!
    I want to every player sits in a chair.
  2. What is the issue? Include screenshots / videos if possible!
explorer

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    So far, I have tried to write scripts.
for i,Player in pairs(game.Players:GetPlayers()) do
		for i,v in pairs(game.Workspace.Main_Room:GetChildren()) do
			if v["Wood Chair"].Owned.Value == false then
				Player.Character.Humanoid:MoveTo(v["Wood Chair"].Seat.Position)
			    v["Wood Chair"].Owned.Value = true
			end
		end
	end

EDIT: Sorry my script have error

3 Likes

You should set the players HumanoidRootPart CFrame to the Seat.

1 Like

You can also use Seat | Documentation - Roblox Creator Hub.

But OP:

  1. Make a table of Seats
  2. Loop through game.Players:GetPlayers():
    • pick a random seat from that seat table
    • put the player in it
    • remove it from the table
    • continue until no more seats or no more players
2 Likes

I tried, have an error:
invalid argument #1 to 'pairs' (table expected, got Instance)

EDIT: Fixed
for i,v in pairs(game.Workspace.Main_Room:GetChildren()) do

You’re looping through each player, and for each player, you loop through every chair. Meaning that only the first player will own a chair (they will teleport through all chairs, then all of the chair will be owned even though no one actually sits on it)

use break after setting the chair:

for i,Player in pairs(game.Players:GetPlayers()) do
		for i,v in pairs(game.Workspace.Main_Room:GetChildren()) do
			if v.Owned.Value == false then
				Player.Character.Humanoid:MoveTo(v.Seat.Position)
			    v.Owned.Value = true
                break;
			end
		end
	end

Edit 3: I don’t know if your teleportation method works, but here’s another one just incase:

Player.Character:SetPrimaryPartCFrame(CFrame.new(v.Seat.Position))

Edit 4: v is actually the woodchair already so you don’t have to do v["Wood Chair"]

2 Likes

You don’t really have to make a table of seats, you can loop through the folder just as fine.

2 Likes

How can I do it?

local Table = {}

for i,v in pairs(game.Workspace.Main_Room:GetChildren()) do

Table = v

end
1 Like
local Table = {}

for i,v in pairs(game.Workspace.Main_Room:GetChildren()) do

table.insert(Table, v)

end
1 Like

I did it. Thanks @nicemike40 @Thanhpro291368 @ItsNickBoi110

local Table = {}
	
	for i,v in pairs(game.Workspace.Main_Room:GetChildren()) do
		table.insert(Table, v)
	end
	
	for i,v in pairs(game.Players:GetPlayers()) do
		repeat
			local RandomChair = Table[math.random(1, #Table)] -- I have 18 chairs
			v.Character:SetPrimaryPartCFrame(CFrame.new(RandomChair.Seat.Position))
			table.remove(Table, Table[RandomChair])
		until #Table == 0
    end

Nice! Here’s a slightly cleaned up version:

local chairs = workspace.Main_Room:GetChildren()

for i, v in pairs(game.Players:GetPlayers()) do
	if #chairs == 0 then break end -- ran out of chairs, stop the loop

	local chairIndex = math.random(1, #chairs)

	local seat = chairs[chairIndex].Seat

	if v.Character then
		local humanoid = v.Character:FindFirstChild("Humanoid")

		if humanoid then
			seat:Sit(humanoid)
			table.remove(chairs, chairIndex) -- don't pick it again
		end
	end
end
2 Likes