Problem with giving player random chair

so basically i have a random number variable (randomNo). say if 1 player gets “Seat1” and the next player gets “Seat1” from the random number it throws an error. how can i make it so that if the seat from the random number is occupied to roll the random number again? sorry if this doesnt make sense. ty!

function startGameModule.startGameFunction()

	game.Players.PlayerAdded:Connect(function(p)
		p.CharacterAdded:Connect(function(c)
			local randomNo = Random.new():NextInteger(1,20)
			wait()
			local seatOFFSET=CFrame.new(0,2,0)

			local seatFolder = workspace.tableModel.seatFolder
			local plateFolder = workspace.tableModel.plateFolder
			local hum:Humanoid = c.Humanoid

			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
					if randomSeat:GetAttribute("occupied") == false 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)
end

randomSeat = seatFolder[`Seat{tostring(randomNo)}`]

repeat
    randomSeat = seatFolder[`Seat{tostring(Random.new():NextInteger(1,20))}`]
until randomSeat:GetAttribute("occupied") ~= false

you might have to change false to true :sob:

1 Like

hey man, tysm 4 ur help!

i tried it like this but its rolling the same random number, (2 twice). i also tried it ur way but it still picks the same number 2 twice.

image

i tried this but it doesnt work:

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)}`]
			
			if randomSeat:GetAttribute("occupied") == true then
				repeat
					randomSeat = seatFolder[`Seat{tostring(randomNo)}`]
				until randomSeat:GetAttribute("occupied") == false
			end

			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

hey man just an update, i got it. u basically got it. i just had to tweak a few things. tysm for ur help bro!!

solution:

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
1 Like

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