If more than 2 players in server do

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Make the script work (cant describe it really sorry.)

  2. What is the issue? Include screenshots / videos if possible!
    Workspace.CurrentFloor.MainScript:9: invalid argument #2 to ‘random’ (interval is empty)
    Line 9

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Devfourms and other code

Code:

task.wait(0.5)

local playerTable = game:GetService("Players"):GetPlayers()
local seatTable = script.Parent.Seats:GetChildren()

local randomPlayer1 = playerTable[math.random(1, #playerTable)] -- selecting our first random player
table.remove(playerTable, 1)

local randomPlayer2 = playerTable[math.random(1, #playerTable)] -- selecting our second random player
table.remove(playerTable, 2)

local randomPlayer3 = playerTable[math.random(1, #playerTable)] -- selecting our third random player
table.remove(playerTable, 3)

local Seat1 = seatTable[math.random(1, #seatTable)] -- selecting our first random seat
table.remove(seatTable, 1)
local Seat2 = seatTable[math.random(1, #seatTable)] -- selecting our second random seat
table.remove(seatTable, 2)
local Seat3 = seatTable[math.random(1, #seatTable)] -- selecting our third random seat
table.remove(seatTable, 3)

if randomPlayer1 and randomPlayer2 and randomPlayer3 and Seat1 and Seat2 and Seat3 then

	local c1 = randomPlayer1.Character
	local c2 = randomPlayer2.Character
	local c3 = randomPlayer3.Character

	local hum1 = c1.Humanoid
	local hum2 = c2.Humanoid
	local hum3 = c3.Humanoid

	-- SEAT 1 --

	if Seat1:GetAttribute("occupied") == false then
		c1:PivotTo(Seat1.CFrame) 

		task.wait(0.5)
		Seat1:Sit(hum1)

		if Seat1.Occupant then
			Seat1:SetAttribute("occupied", true)
			Seat1:SetAttribute("playerName", Seat1.Occupant.Parent.Name)
		end
	end

	-- SEAT 2 --

	if Seat2:GetAttribute("occupied") == false then
		c1:PivotTo(Seat2.CFrame) 

		task.wait(0.5)
		Seat2:Sit(hum2)

		if Seat2.Occupant then
			Seat2:SetAttribute("occupied", true)
			Seat2:SetAttribute("playerName", Seat2.Occupant.Parent.Name)
		end
		table.remove(seatTable, 2)
		table.remove(playerTable, 2)
	end

	-- SEAT 3 --

	if Seat3:GetAttribute("occupied") == false then
		c1:PivotTo(Seat3.CFrame) 

		task.wait(0.5)
		Seat3:Sit(hum3)

		if Seat3.Occupant then
			Seat3:SetAttribute("occupied", true)
			Seat3:SetAttribute("playerName", Seat3.Occupant.Parent.Name)
		end
		table.remove(seatTable, 3)
		table.remove(playerTable, 3)
	end
end

Yes the code is messy. tryna get it working before its improved.

When you solo play test, there is only one player in the server, so when you remove the first player from the table, there will be 0 players left to use your random picker on. This is why the second time you pick a player, the script errors. You can fix this by not executing the code if there are not at least 3 players in the table:

if #playerTable < 3 then
	return
end

Now you would probably want to make sure to connect this to PlayerAdded so that you can you can start the script whenever you have at least 3 players in total:

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function()
	local playerTable = Players:GetPlayers()
	if #playerTable < 3 then
		return
	end
	
	-- Insert the rest of your code here to initiate whatever you are doing
end)

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