Putting players into seats when joined not working

I am making a script where when the players join into the match/game, it puts them into individual seats. There are 18 seats and if 18 players are in the match, the chairs must be full. My script has an error that is like this:


Here is my script:

local Players = game:GetService("Players")
local chair = script.Parent
local seats = {}


for _, child in chair:GetChildren() do
    if child:IsA("Seat") then
        table.insert(seats, child)
    end
end

local playerSeatMap = {}
local seatsAssigned = false

Players.PlayerAdded:Connect(function(player)
    if seatsAssigned then return end

    local character = player.Character or player.CharacterAdded:Wait()

    for _, seat in seats do
        if not seat.Occupant then
            playerSeatMap[player] = seat
            seat.Occupant = character:WaitForChild("Humanoid")
            break
        end
    end

    for _, existingPlayer in Players:GetPlayers() do
        if existingPlayer ~= player then
            task.spawn(function()
                for _, seat in seats do
                    if not seat.Occupant then
                        playerSeatMap[existingPlayer] = seat
                        seat.Occupant = existingPlayer.Character:WaitForChild("Humanoid")
                        break
                    end
                end
            end)
        end
    end
    
    seatsAssigned = true
end)

Players.PlayerRemoving:Connect(function(player)
    local seat = playerSeatMap[player]
    if seat then
        seat.Occupant = nil
        playerSeatMap[player] = nil
    end
end)
1 Like

Try Seat.Sit(HumanoidObject)

Where do I put this? 300 characters

Replace the lines with ‘seat.Occupant =’ since Occupant’s a read only property (which I think you’ve seen enough of by now lol)

I see about 3 instances of that.
You can just remove the line with ‘seat.Occupant = nil’ since I think it auto sets to nil.

1 Like

But now it is not putting me in the seat

Try adding something like ‘task.wait(5)’ before sitting since I had some issues before with seats not seating me correctly.

Also I made a typo… It was seat:Sit() not seat.Sit() LOL
Try this first ^^^

2 Likes

It finally sits me down but it makes only one person sit down and the other stand up

Here is the script I have

local Players = game:GetService("Players")
local chair = script.Parent
local seats = {}


for _, child in chair:GetChildren() do
	if child:IsA("Seat") then
		table.insert(seats, child)
	end
end

local playerSeatMap = {}
local seatsAssigned = false

Players.PlayerAdded:Connect(function(player)
	if seatsAssigned then return end

	local character = player.Character or player.CharacterAdded:Wait()

	for _, seat in seats do
		if not seat.Occupant then
			playerSeatMap[player] = seat
			character:WaitForChild("Humanoid")
			task.wait(0.01)
			seat:Sit(character.Humanoid)
			break
		end
	end

	for _, existingPlayer in Players:GetPlayers() do
		if existingPlayer ~= player then
			task.spawn(function()
				for _, seat in seats do
					if not seat.Occupant then
						playerSeatMap[existingPlayer] = seat
						task.wait(5)
						seat:Sit(character.Humanoid)
						break
					end
				end
			end)
		end
	end

	seatsAssigned = true
end)

Players.PlayerRemoving:Connect(function(player)
	local seat = playerSeatMap[player]
	if seat then
		seat.Occupant = nil
		playerSeatMap[player] = nil
	end
end)

Try removing the line

seatsAssigned = true

Since it’s for every player and not just one, you never reset it and the second player who joins will unfortunately stand for all eternity.

Also I think you can remove the task.wait(5) unless it breaks again.

Still makes one person stand up

You can also maybe remove this too since every player is seated when they join. Also may I ask what this script or game’s about?

Btw remove seat.Occupant = nil in the PlayerRemoving event.

As for the standing up issue, are they being teleported to the seat and then stand up?

I removed task.wait and it just teleported a single player to the seats.

The game called “Sacrificer”. The game is about surviving a bunch of games without dying. Once they join, they queue up and get teleported to the match. Then they get into a bunch of CHAIRS waiting for the others. And then explains how to play from the narrator.

Now look at this

What the baloney.

Also interesting stuff (for the single teleport and gameplay)

What’s your current, most up-to-date script right now? So that I can find out what to edit.

1:29 AM on a school night. I’m not locked in. :pensive:

local Players = game:GetService("Players")
local chair = script.Parent
local seats = {}


for _, child in chair:GetChildren() do
	if child:IsA("Seat") then
		table.insert(seats, child)
	end
end

local playerSeatMap = {}
local seatsAssigned = false

Players.PlayerAdded:Connect(function(player)
	if seatsAssigned then return end

	local character = player.Character or player.CharacterAdded:Wait()

	character:WaitForChild("Humanoid").JumpHeight = 0
	for _, seat in seats do
		if not seat.Occupant then
			playerSeatMap[player] = seat
			character:WaitForChild("Humanoid")
			task.wait(0.05)
			seat:Sit(character.Humanoid)
			break
		end
	end

	for _, existingPlayer in Players:GetPlayers() do
		if existingPlayer ~= player then
			task.spawn(function()
				for _, seat in seats do
					if not seat.Occupant then
						playerSeatMap[existingPlayer] = seat
						task.wait(0.05)
						seat:Sit(character.Humanoid)
						break
					end
				end
			end)
		end
	end


end)

Players.PlayerRemoving:Connect(function(player)
	local seat = playerSeatMap[player]
	if seat then
		playerSeatMap[player] = nil
	end
end)

Time to try a few things:

  1. Remove this chunk of code for now since it seems unnecessary? (Unless it reorganizes them or something)
  1. Edit the task.wait() times to something like 10-15 to allow everything to load.

  2. Add print(“a”) print(“b”) print(“c”) and etc to whereever the code might be stopping or just for checks.

	for _, existingPlayer in Players:GetPlayers() do
		print("a")
		if existingPlayer ~= player then
			print("b")
			task.spawn(function()
				for _, seat in seats do
					print("c")
					if not seat.Occupant then
						print("d")
						playerSeatMap[existingPlayer] = seat
						task.wait(0.05)
						seat:Sit(character.Humanoid)
						break
					end
				end
			end)
		end
	end


It only prints in the server side

It should be printing in the server output since it’s a serverscript.

I think the issue might be because the humanoid or something else hasn’t loaded in fully.

OK I JUST SAW SOMETHING


They were all loaded in at the start, but player 4 suddenly jumped up. The task.wait() was 10 seconds

Oh-?

Try editing that section of the code which may have caused the jump a bit.

I don’t know much about seat objects, but I’m learning something new every day… :person_shrugging:
(HOLY COW IM GETTING 4 HOURS OF SLEEP TONIGHT WHOO)

Could it be because the player’s already seated and it’s being forced to sit in another seat?