Problem with randomizing

hello! my script was working the other day. it would give a random seat and plate to a player but it would give say eg. Plate1 and Seat1 to the player which is what i wanted but now it has changed.

it gives a completely different plate to the player. (there are 20 plates and 20 seats)
eg:
image

script:

local M6D:Motor6D = nil
local randomSeat:Seat = nil
local randomPlate = nil
local randomNo = Random.new():NextInteger(1, 20)

function startGameModule.startGameFunction()

	game.Players.PlayerAdded:Connect(function(p)
		p.CharacterAppearanceLoaded:Connect(function(c)
			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:GetChildren()[randomNo]
			randomPlate = plateFolder:GetChildren()[randomNo]
							print(randomSeat)
				print(randomPlate)

			if randomSeat:GetAttribute("occupied") == false then
				c:PivotTo(randomSeat.CFrame* seatOFFSET) 
				wait()
				randomSeat:Sit(hum)
				randomPlate.Parent = c
				M6D.Part1 = randomPlate
				if randomSeat.Occupant then
					randomSeat:SetAttribute("occupied", true)
					randomSeat:SetAttribute("playerName", randomSeat.Occupant.Parent.Name)
					randomPlate:SetAttribute("playerName", randomSeat.Occupant.Parent.Name)
					randomPlate:SetAttribute("occupied", true)
					c:SetAttribute("sitAttribute", true)
				end
			end	
		end)
	end)
end
3 Likes

are the seat and the plate supposed to be different? If that is the issue, then just link a plate to a seat. So, when you find the plate, it will just find the seat linked to it.

3 Likes

hey man, ty 4 ur help.

basically im trying to have the seat the same as the plate so if the player gets seat1, he will get plate1 as well but im getting this:

image

sometimes it works but sometimes it gives me the previous plate.
image

1 Like

Yeah, try making the seat or plate in the same model or something like that.

3 Likes

children don’t necessarily have the same indexes for chairs and plates
try combining them somehow
e.g name each of them with the number
chair - 1, plate - 1

3 Likes

yes well i have my seats and plates well organized tbh w u bro but idek what’s going on :sob:

1 Like

i tried something like this just now but i cant really like connect it up to give the player a random seat + plate.


local plateFolder = workspace.tableModel.plateFolder
local seatFolder = workspace.tableModel.seatFolder

local seatsTable = {
s1 = seatFolder.Seat01,
	s2 = seatFolder.Seat02,
	s3 = seatFolder.Seat03,
	s4 = seatFolder.Seat04,
	s5 = seatFolder.Seat05,
	s6 = seatFolder.Seat06,
	s7 = seatFolder.Seat07,
	s8 = seatFolder.Seat08,
	s9 = seatFolder.Seat09,
	s10 = seatFolder.Seat10,
	s11 = seatFolder.Seat11,
	s12 = seatFolder.Seat12,
	s13 = seatFolder.Seat13,
	s14 = seatFolder.Seat14,
	s15 = seatFolder.Seat15,
	s16 = seatFolder.Seat16,
	s17 = seatFolder.Seat17,
	s18 = seatFolder.Seat18,
	s19 = seatFolder.Seat19,
	s20 = seatFolder.Seat20,
}

local PlatesTable = {
	p1 = plateFolder.plate01,
	p2 = plateFolder.plate02,
	p3 = plateFolder.plate03,
	p4 = plateFolder.plate04,
	p5 = plateFolder.plate05,
	p6 = plateFolder.plate06,
	p7 = plateFolder.plate07,
	p8 = plateFolder.plate08,
	p9 = plateFolder.plate09,
	p10 = plateFolder.plate10,
	p11 = plateFolder.plate11,
	p12 = plateFolder.plate12,
	p13 = plateFolder.plate13,
	p14 = plateFolder.plate14,
	p15 = plateFolder.plate15,
	p16 = plateFolder.plate16,
	p17 = plateFolder.plate17,
	p18 = plateFolder.plate18,
	p19 = plateFolder.plate19,
	p20 = plateFolder.plate20
}

local randomSeat:Seat = nil
local randomPlate = nil
local randomNo = Random.new():NextInteger(1, 20)

function startGameModule.startGameFunction()

	game.Players.PlayerAdded:Connect(function(p)
		p.CharacterAppearanceLoaded:Connect(function(c)
			wait()

			local seatOFFSET=CFrame.new(0,2,0)
			local hum:Humanoid = c.Humanoid
			
			randomSeat = seatsTable[randomNo]
			randomPlate = PlatesTable[randomNo]
							print(randomSeat)
				print(randomPlate)

			if randomSeat:GetAttribute("occupied") == false then
				c:PivotTo(randomSeat.CFrame* seatOFFSET) 
				wait()
				randomSeat:Sit(hum)
				--randomPlate.Parent = c
				--M6D.Part1 = randomPlate
				if randomSeat.Occupant then
					randomSeat:SetAttribute("occupied", true)
					randomSeat:SetAttribute("playerName", randomSeat.Occupant.Parent.Name)
					randomPlate:SetAttribute("playerName", randomSeat.Occupant.Parent.Name)
					randomPlate:SetAttribute("occupied", true)
					c:SetAttribute("sitAttribute", true)
				end
			end	
		end)
	end)
end

and yeah…
image

2 Likes

Maybe only randomize the seats but not the plates, make it so if the player gets seat 1 then he’ll get plate 1

2 Likes

This is because :GetChildren() returns a table with randomly indexed instances. I would recommend you set an attribute to each of the plates and seats for their order or just name them by number. Then, you could do:

randomSeat = seatFolder[tostring(randomNo)] -- could also be findifrstchild
randomPlate = plateFolder[tostring(randomNo)]
2 Likes

hey man,

thats very valuable info. tysm!

one problem is i dont really know how to connect the attribute up to the plates/seats. how would u do that?

ive got a “number” attribute now:
image

1 Like

What do you mean connect the attribute? Just set the corresponding plate to the same number (also, it seems like your seats and plates are already numbered, you could do this):

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

If you really want to do attributes, do this:

for _, plate in pairs(plateFolder:GetChildren()) do
      if plate:GetAttribute("Number") == randomNo then
            randomPlate = plate
            break
      end
end
for _, seat in pairs(seatFolder:GetChildren()) do
      if seat:GetAttribute("Number") == randomNo then
            randomSeat = seat
            break
      end
end

u absolute legend. that would’ve taken me weeks to figure out. im not so familiar w the symbols and stuff so i have to learn about them. i cant thank u enough man. thank u so much for ur help bro!!! :slight_smile:

solution:

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)
						randomSeat:SetAttribute("playerName", randomSeat.Occupant.Parent.Name)
						randomPlate:SetAttribute("playerName", randomSeat.Occupant.Parent.Name)
						randomPlate:SetAttribute("occupied", true)
						randomPlate.Parent = c
					end
				end

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

image

instead of this you could put this in the table (not a list)
{chair1, char2, char3}
{plate1, plate2, plate3}

then the random index will be used for both
local random_index = …

local chair = chairs[random_index]
local plate = plates[random_index]

1 Like

jesus i didnt see ur post till now man. thank u so much for ur help tho. i didnt really understand what u meant until now. ty1!!!

thats a great solution also. i didn’t know u could do it that way. i bookmarked this for future references. tysm man, have a great day!!! :slight_smile:

1 Like

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