Best way to randomize

I am making a elevator game similar to the normal elevator and I am wondering the best way to randomize floors without repeats of the same floor here is the script.

local IntermissionTime = 10
local RoundTime = 15
local Time = game.ReplicatedStorage.Values.Time
local InRound = game.ReplicatedStorage.Values.InRound
local DoorTime = 2.5
InRound.Value = false
local maps = game.ReplicatedStorage.Maps
local telepart = workspace.Elevator.telepart
local wall = workspace.Elevator.Doors.wall
local NoTele = false

wait(2)

InRound.Changed:Connect(function()
if InRound.Value == true then
map = math.random(1,3)
print(map)
if map == 1 then
RoundTime = 15
mapclone = maps.TacoStand:Clone()
mapclone.Parent = workspace
elseif map == 2 then
RoundTime = 5
mapclone = maps.map_gaming:Clone()
mapclone.Parent = workspace
wall.CanCollide = true
NoTele = true
elseif map == 3 then
RoundTime = 15
mapclone = maps.TestMap3:Clone()
mapclone.Parent = workspace
elseif map == 4 then
RoundTime = 15
mapclone = maps.TestMap2:Clone()
mapclone.Parent = workspace
end
else

	wall.CanCollide = true
	if NoTele == false then
	for _, player in pairs(game.Players:GetChildren()) do
		local char = player.Character
		char.HumanoidRootPart.CFrame = telepart.CFrame
		end
		end
	wait(5)
	mapclone:Destroy()
	wall.CanCollide = false
	NoTele = false
end
end)

local function timerfunction()
while wait() do
for i = IntermissionTime, 0, -1 do
wait(1)
Time.Value = i
end
print(“opendoor”)
InRound.Value = true
wait(DoorTime)
for i = RoundTime, 0, -1 do
wait(1)
Time.Value = i
end
print(“door close”)
InRound.Value = false
wait(DoorTime)
end
end

spawn(timerfunction)

2 Likes

Track the last rooms and if the random number picked is similar then pick another number until it doesn’t match the last 10 rooms or any of the last rooms

1 Like

Or you could the insert the number it into a table and remove it, once the function is called, this way the number doesn’t get repeated again. Also please consider formatting you code, so that it’s readable.

3 Likes

I tried this by assigning each room a number value to a variable called lastroom and made
map = math.random(1,2)
if map == lastroom then
return
end

sorry if this question sounds dumb but how would I make it pick another number.

I inserted the number using a table named roomtable and made it insert the number into the table using table.insert(roomtable,map). Im sorry if this is an amateur question but what would I do after I have the number stored?

I’m not sure I understand your coding because it’s a bit confusing. Does your map variable have all the floor as its children.

No, map is the variable used as the math.random variable code: map = math.random(1,3) sorry that I made it so confusing.

Alright so map is the map is the floor that you get teleported to right?

It decides what floor you land on yes.

If it helps I can post a simplified version of what the script looks like so you can have a better understanding

map = math.random(1,3) --random number
table.insert(roomtable,map) --[[roomtable is a predefined table and I am adding the random number to the table–]]

	if map == 1 then
		RoundTime = 5
		mapclone = maps.TacoStand:Clone()
		mapclone.Parent = workspace
		lastroom = 1

	elseif map == 2 then
		RoundTime = 5
		mapclone = maps.map_gaming:Clone()
		mapclone.Parent = workspace
		wall.CanCollide = true
		NoTele = true
		lastroom = 2

	elseif map == 3 then
		RoundTime = 15
	mapclone = maps.TestMap3:Clone()
		mapclone.Parent = workspace
		lastroom = 3

	elseif map == 4 then
	RoundTime = 15
	mapclone = maps.TestMap2:Clone()
		mapclone.Parent = workspace
		lastroom = 4

yes, i was about to finish the code but i was lost on the way.

Thank you for sticking with me and my amateur coding ability.

no worries, but you would have to do something like
for your math.random you can say

local AvailableMaps = Maps:GetChildren()
local chosenMap = AvailableMaps[math.random(1,#AvailableMaps)]

-- then you would say

if map == 1 then
table.remove(Maps,1)

Edit : I don’t think the last bit is right. I’m not good of a scripter myself

I don’t think I inserted everything correctly because it didn’t work. This is what I did:

local maps = game.ReplicatedStorage.Maps

local AvailableMaps = maps:GetChildren()

local map = AvailableMaps[math.random(1,#AvailableMaps)]

	if map == 1 then
		table.remove(map,1)
		RoundTime = 5
		mapclone = maps.TacoStand:Clone()
		mapclone.Parent = workspace
		lastroom = 1

	elseif map == 2 then
		RoundTime = 5
		mapclone = maps.map_gaming:Clone()
		mapclone.Parent = workspace
		wall.CanCollide = true
		NoTele = true
		lastroom = 2

	elseif map == 3 then
		RoundTime = 15
	mapclone = maps.TestMap3:Clone()
		mapclone.Parent = workspace
		lastroom = 3

	elseif map == 4 then
	RoundTime = 15
	mapclone = maps.TestMap2:Clone()
		mapclone.Parent = workspace
		lastroom = 4

also do I have to do table.remove for each map selected?

Ah, I need to learn more about tables. Try do it for each map selected, but i don’t think the table.remove is right. I’ll see if i can find a way.

It didn’t work and I guess need to learn more about tables too. Sorry for wasting your time.

This might help table | Documentation - Roblox Creator Hub

I’m not sure but maybe try “table.remove(AvailableMaps)”. Can you put prints into your code so i can see where I went wrong.

if you print tables wont it come up as “table 3123123” or something like that

no it doesn’t come up like that.