Trying to prevent same random number

so i have a script that everytime a player joins they get a random seat but if a player has seat 2 and another player has seat 2 it throws an error.

how can i get a new random number if say a player is on the seat? i appreciate the help!!

function startGameModule.startGameFunction()

	game.Players.PlayerAdded:Connect(function(p)
		p.CharacterAdded:Connect(function(c)
			local seatOFFSET=CFrame.new(0,2,0)

			local seatFolder = workspace.tableModel.seatFolder
			local plateFolder = workspace.tableModel.plateFolder
			local hum:Humanoid = c.Humanoid
			randomNo = Random.new():NextInteger(1,2)
print(randomNo)
			wait()
			
			randomSeat = seatFolder[`Seat{tostring(randomNo)}`]
			randomPlate = plateFolder[`Plate{tostring(randomNo)}`]
			
			repeat
				randomSeat = seatFolder[`Seat{tostring(Random.new():NextInteger(1,2))}`]
				wait(.1)
			until randomSeat:GetAttribute("occupied") ~= true

			if randomSeat:GetAttribute("occupied") == false then
				c:PivotTo(randomSeat.CFrame * seatOFFSET)
				wait()
				randomSeat:Sit(hum)

				if randomSeat.Occupant then
						randomSeat:SetAttribute("occupied", true)
						c:SetAttribute("sitAttribute", true)
						randomSeat:SetAttribute("playerName", randomSeat.Occupant.Parent.Name)
						randomPlate:SetAttribute("playerName", randomSeat.Occupant.Parent.Name)
						randomPlate:SetAttribute("occupied", true)
						randomPlate.Parent = c
						M6D.Part1 = randomPlate

						print({
							p.Name.. randomSeat.Name,
							p.Name.. randomPlate.Name
						})
				end
			end
		end)
	end)
end

just an update sorry to those replying, i got it!!

this is the solution for those who’re trying to achieve something similar:

local M6D:Motor6D = nil
local randomSeat:Seat = nil
local randomPlate = nil
local randomNo = nil
local numberTable = {}

function startGameModule.CreateM6D()
	game.Players.PlayerAdded:Connect(function(p)
		p.CharacterAppearanceLoaded:Connect(function(c)
			if c:FindFirstChild("plateM6D") then return end
			M6D = Instance.new("Motor6D", c)
			M6D.Name = "plateM6D"
		end)
	end)
end

function startGameModule.startGameFunction()

	game.Players.PlayerAdded:Connect(function(p)
		p.CharacterAdded:Connect(function(c)
			local seatOFFSET=CFrame.new(0,2,0)

			local seatFolder = workspace.tableModel.seatFolder
			local plateFolder = workspace.tableModel.plateFolder
			local hum:Humanoid = c.Humanoid
			randomNo = Random.new():NextInteger(1,20)
			
			print(randomNo.." is the seat number!")

if table.find(numberTable, randomNo) == true then
	repeat
					randomNo = Random.new():NextInteger(1,20)
					wait(.1)
	until table.find(numberTable, randomNo) == nil
end

			table.insert(numberTable, randomNo)

			wait()
			
			randomSeat = seatFolder[`Seat{tostring(randomNo)}`]
			randomPlate = plateFolder[`Plate{tostring(randomNo)}`]

			if randomSeat:GetAttribute("occupied") == false then
				c:PivotTo(randomSeat.CFrame * seatOFFSET)
				wait()
				randomSeat:Sit(hum)

				if randomSeat.Occupant then
						randomSeat:SetAttribute("occupied", true)
						c:SetAttribute("sitAttribute", true)
						randomSeat:SetAttribute("playerName", randomSeat.Occupant.Parent.Name)
						randomPlate:SetAttribute("playerName", randomSeat.Occupant.Parent.Name)
						randomPlate:SetAttribute("occupied", true)
						randomPlate.Parent = c
						M6D.Part1 = randomPlate

						print({
							p.Name.. randomSeat.Name,
							p.Name.. randomPlate.Name
						})
				end
			end
		end)
	end)
end

very helpful post:

1 Like

I would make a table/dictionary and remove the chair from the table if someone is sitting in it. Then to select a chair do

Math.random(1,#table)

Obviously change table in the example to your table

1 Like

hey man, you were extremely close!!!

basically u were right about the table:

table:

local numberTable = {}

random number variable:

			randomNo = Random.new():NextInteger(1,20)
			
			print(randomNo.." is the seat number!")

if the seat number is in the table then roll the dice again basically until it finds a number not in the table:

if table.find(numberTable, randomNo) == true then
	repeat
					randomNo = Random.new():NextInteger(1,20)
					wait(.1)
	until table.find(numberTable, randomNo) == nil
end

insert the new number that isnt in the table

			table.insert(numberTable, randomNo)

and remove the seat number from the table when someone leaves so the next person who joins can sit there:

			table.insert(numberTable, randomNo)
1 Like

lol, I just told you how I would’ve done it. But yeah the best part about programming is that there is no one way to do something

1 Like