Door Randomizer Issue

Hi everyone, i’m trying to make a game with some minigames inside, in one of them you need to go through some Doors Lines and go through the one who is opened

(Do you remember Takeshi’s Castle or Fall Guys level? Is similiar to that)

What shoud happen with this level so?
There are like 8 lines of 7 Doors each in this level, the script should:

Take randomically 3 of the 7 doors and lock them (Just anchor and change their color with red, for tests)

Picture 1

The other 4 doors will be Not anchored and changed to Green color (always for tests)

Picture2

In the other 4 green doors 1 or 2 will have a trap inside them (Exactly as Takeshi’s Castle)
And will change their color to Cyan/Blue

Picture3

3

And this will repeat for the remaining 7 lines

Why are you doing this so complex?
To make the minigame change everytime so people can’t memorize the correct path and win so easily, that will ruin the competition between players.

The issue:
Actually i’m trying making my own script for this and i know that what i’ve done is really complex and does not even work fine so

I Am Once Again Asking for Your Scripting Support

My actual Script
LineNumber = math.random(1,1)
LockNumber = 0
OpenNumber = 0
TrapNumber = 0

doing = 0

if LineNumber == 1 then
		repeat
		doing = doing +1
		OldNumber = LockNumber
		LockNumber = math.random(1,7)
		if OldNumber == LockNumber then
			print("Oh shit")
			doing = doing -1
		end
		
		print("Randomized Number: " ..LockNumber)
		print("Old Number: " ..OldNumber)
		print("Doing number: " ..doing)
		print("----------------------") --Just to make space between Numbers..
		wait(3)
		
		if doing == 1 then
			LockedDoor1 = LockNumber
		elseif doing == 2 then
			LockedDoor2 = LockNumber
		elseif doing == 3 then
			LockedDoor3 = LockNumber
		end
	until doing == 3
	
	
		print(LockedDoor1)
		print(LockedDoor2)
		print(LockedDoor3)

		
			if LockedDoor1 or LockedDoor2 or LockedDoor3 == 1 then
			print("Why is always printed?")
			elseif LockedDoor1 or LockedDoor2 or LockedDoor3 == 2 then
			elseif LockedDoor1 or LockedDoor2 or LockedDoor3 == 3 then
			elseif LockedDoor1 or LockedDoor2 or LockedDoor3 == 4 then
			print("Test")
			elseif LockedDoor1 or LockedDoor2 or LockedDoor3 == 5 then
			elseif LockedDoor1 or LockedDoor2 or LockedDoor3 == 6 then
			elseif LockedDoor1 or LockedDoor2 or LockedDoor3 == 7 then
			end
	
	
	
	
	
	
	
end

NOTE: The Doors are composed by 4 parts to make them look like are being crashed so every door is actually a model with 4 parts inside.

1 Like

I’m not experienced with reading other people’s code so I can’t really do that, but I can tell you a good way to script this:

  1. Have a table with all 7 doors inside it.
  2. Get 3 random numbers from 1 to 7, make the doors in those indexes of the table locked and remove those doors from the table.
  3. Change the rest of the doors to green, but don’t remove them from the table yet (so we can pick trap doors)
  4. Get 2 random numbers from 1 to 4, change the doors in those indexes of the table to trap doors.
  5. Now you can clear the table, and repeat the same for the next line of doors.

To iterate through the lines of doors you can use for loops.
To get DIFFERENT random numbers and not the same ones you can you repeat until loops.

Hope this helps!
P.S. I recommend learning about tables first, because that’s the main thing you’re gonna use.

I’ll sure study a lot about tables until i’ll master them, about changing all the parts inside a model i can use :GetChildren(Code) or there is another way?

Model:GetChildren() would be a good idea. Store all the children inside a table, and loop through it to change all the parts’ properties.

Post your code here please, so we can help.

He did, and I already gave him a solution.

Alright, sorry I must’ve read it wrong.

The biggest problem with your existing code is that a conditional like this:

probably isn’t checking what you want. Were you meaning to test something like this:
if LockedDoor1==1 or LockedDoor2==1 or LockedDoor3==1 then
or were you actually trying to test that LockedDoor1 and LockedDoor2 are uninitialized (so nil) or set to false? You don’t show where these are declared, so impossible for us to tell.

Overall, you can probably implement this who system without a single if .. then conditional, just by making an array of 7 values, e.g. { “locked”,“locked”, “locked”,“open”,“open”,“trapped”,“trapped” } and shuffling it with any simple shuffling algorithm. You can shuffle like:
table.sort(a, function(a,b) return math.random()<0.5 end)
or do something more like implement a Fisher-Yates type swap (still only like 4 lines of code).

1 Like

Heya! So, i made a simple system for the Doors and this is actually working! The only problem now is: how do i edit the parts inside the model? I mean, models like “Door1, Door2, etc” are actually “TrappedDoors[1]” or “LockedDoors[3]” And i’m searching for how to edit them

Actual code:
L1L = workspace.Working.DoorsLine1 --L1L is for Line1Location

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

LockedDoors = {}

TrappedDoors = {}

m = 0

b = 7

m2 = 0

b2 = 4

print("The extracted numbers are:")

repeat

m = m +1

b = b -1

n = math.random(1,b)

table.move(OpenDoors,n,n,m,LockedDoors)

table.remove(OpenDoors,n)

wait(1)

until m == 3

repeat

m2 = m2 +1

b2 = b2 -1

n2 = math.random(1,b2)

table.move(OpenDoors,n2,n2,m2,TrappedDoors)

table.remove(OpenDoors,n2)

wait(1)

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])
Actual Output:

1

Actual Parts Location:

2

If you want to edit parts inside a model, do this:

local partsTable = Model:GetChildren()

for i = 1, #partsTable do
      partsTable[i].Position = Vector3.new(0, 0, 0)
end

I just changed the position in this example.

I’ve actually made this but i’ll try your script too, seems less complex and i guess it can do the exact thing i made here, now i’m trying to figure out how to correctly place the traps but thank you so much for the Table Solution :smile:

1

I used normal for loops because I thought you didn’t know what in pairs for loops are, but they’re both perfectly fine in this example.