Door randomizer script issue

Hi everyone! i’ve made a door randomizer for a minigame wich takes 3 locked doors, 2 trapped doors and 2 unlocked doors for every row of doors

The issue: i would like to change a little bit my script and add the possibility to have 0 to 2 trapped doors and so on with the locked ones, so the number will be always randomized and not always the same number of locked/trapped doors, here is the script i’m using

function Line()

local OpenDoors = {L1L.Door1, L1L.Door2, L1L.Door3, L1L.Door4, L1L.Door5, L1L.Door6, L1L.Door7}
local LockedDoors = {}
local TrappedDoors = {}

local m = 0
local b = 8
local m2 = 0
local b2 = 5
print("The extracted numbers are:")

repeat
	m = m +1
	b = b -1
	
	local n = math.random(1,b)

	table.move(OpenDoors,n,n,m,LockedDoors)	
	table.remove(OpenDoors,n)
	wait(.5)
until m == 3

repeat
	m2 = m2 +1
	b2 = b2 -1
	
	local n2 = math.random(1,b2)
	
	table.move(OpenDoors,n2,n2,m2,TrappedDoors)
	table.remove(OpenDoors,n2)
	wait(.5)
until m2 == 2


print("----------")
print("Locked Doors:")
print(LockedDoors[1])
print(LockedDoors[2])
print(LockedDoors[3])
print("----------")
print("Opened Doors:")
print(OpenDoors[1])
print(OpenDoors[2])
print("----------")
print("Trapped Doors:")
print(TrappedDoors[1])
print(TrappedDoors[2])
print("----------")

for Door, Part in pairs(LockedDoors) do
	Part.Part1.BrickColor = BrickColor.new("Really red")
	Part.Part2.BrickColor = BrickColor.new("Really red")
	Part.Part3.BrickColor = BrickColor.new("Really red")
	Part.Part4.BrickColor = BrickColor.new("Really red")
	
	Part.Part1.Anchored = true
	Part.Part2.Anchored = true
	Part.Part3.Anchored = true
	Part.Part4.Anchored = true


end


for Door, Part in pairs(OpenDoors) do
	Part.Part1.BrickColor = BrickColor.new("Bright green")
	Part.Part2.BrickColor = BrickColor.new("Bright green")
	Part.Part3.BrickColor = BrickColor.new("Bright green")
	Part.Part4.BrickColor = BrickColor.new("Bright green")
	
	Part.Part1.Anchored = false
	Part.Part2.Anchored = false
	Part.Part3.Anchored = false
	Part.Part4.Anchored = false


end


for Door, Part in pairs(TrappedDoors) do
	Part.Part1.BrickColor = BrickColor.new("Cyan")
	Part.Part2.BrickColor = BrickColor.new("Cyan")
	Part.Part3.BrickColor = BrickColor.new("Cyan")
	Part.Part4.BrickColor = BrickColor.new("Cyan")
	
	Part.Part1.Anchored = false
	Part.Part2.Anchored = false
	Part.Part3.Anchored = false
	Part.Part4.Anchored = false
	
	local OriginalTrap = script.Parent.Traps.OriginalTrap
	local Copy = OriginalTrap:Clone()
	

	Copy.Parent = OriginalTrap.Parent
	local OriginalPositionX = Part.PrimaryPart.Position.x
	local OriginalPositionY = Part.PrimaryPart.Position.y
	local OriginalPositionZ = Part.PrimaryPart.Position.z
	
	local NewPositionX = OriginalPositionX +13
	local NewPositionY = OriginalPositionY -6
	local NewPositionZ = OriginalPositionZ -3
	
	print(OriginalPositionX,OriginalPositionY,OriginalPositionZ)
	print(NewPositionX,NewPositionY,NewPositionZ)
	
	Copy:MoveTo(Vector3.new (NewPositionX,NewPositionY,NewPositionZ))
	
	end
end

As you can see the (m) variable is the number of how many doors will be in that row, so can be changed to 1 and 2 without problem but when i try to change

until m2 == 2

to

until m2 == math.random(1,2)

i have an error, does anyone know how to make this?

Here is a video on how this script works

robloxapp-20200824-1647219.wmv (661,7 KB)

1 Like

When you use until m2 == 2 your script can produce the exact result based on it. However when you compare until m2 == math.random(1,2) your script is not always find a result.

Example

-- you have this by default
local m2 = 0
repeat
	m2 = m2 +1
	-- skipped
until m2 == math.random(1,2)

In the first iteration your m2 = 1 but the math.random(1,2) can be 2, so no match.
In the second iteration your m2 = 2 but the math.random(1,2) can be 1, so again no match.
Once your m2 did not pass the second iteration it will not find the result until

	b2 = b2 -1
	local n2 = math.random(1,b2)

b2 will 1 and you will get error for passing it as argument in math.random(1,1)

So what i have to do to make it functional?

Maybe make the m2 back to 0 every 2 iterations, and b2 back to 5 when it reached 2. Something like this

repeat
	if m2 == 2 then m2 = 0 end	-- add this line
	if b2 == 2 then b2 = 5 end	-- add this line
	m2 = m2 +1
	b2 = b2 -1
	-- skipped
until m2 == math.random(1,2)

if m2 == 2 then m2 = 0 end because when m2 = 2 meaning your math random can’t find a match so reset the value to increase the chances.

if b2 == 2 then b2 = 5 end to avoid error just incase your chances is taking longer than expected.