How would i get the number of times a For Loop has ran?

  1. What do you want to achieve?
    I am making a series of generated rooms and generating multiple of them with a for loop:
local room = require(script.Room) -- The module script that generates rooms.

local prevRoom = game.Workspace.Start -- A variable for calling the room generation function.

for i = 1, 100 do -- Heres the for loop.
	prevRoom = room.Generate(prevRoom) -- Calling the generate room function from the module script.
end

But now I want to detect the number its on in the for loop.
The reason why I want to detect the number is so i can add a custom room for that number.

  1. What is the issue?
    I cannot figure out how and i have tested many solutions and none of them seem to work.

  2. What solutions have you tried so far?
    I have tried making a number value that changes each time in the for loop and detecting if that number is the number i want the custom room on. If it was that number then I made it call a function in the Module Script that generates the special room but that does not seem to work.

Here is all of the code and progress I have so far.

-- Server Script
local room = require(script.Room)

local prevRoom = game.Workspace.Start

local number = script.RoomValue.Value -- The number value that tracks it.

for i = 1, 100 do
        if number ~= 52 then
	       prevRoom = room.Generate(prevRoom) -- Calling the function
        else
                prevRoom = room.Special(prevRoom, game.Workspace.Rooms.Shop)
        end
end

-- Module Script
local room = {}

room.random = Random.new()
room.lastTurnDirection = nil
room.info = require(script.roomInfo)

function room.GetRandom(prevRoom)
	local possibleRooms = game.Workspace.Rooms:GetChildren()
	local totalWeight = 0
	for i, info in pairs(room.info) do
		totalWeight += info.Weight
	end
	
	local randomWeight = room.random:NextNumber(1, totalWeight)
	local currentWeight = 0
	local randomRoom = nil
	for i, info in pairs(room.info) do
		currentWeight += info.Weight
		if randomWeight <= currentWeight then
			randomRoom = workspace.Rooms[i]
			break
		end
	end
	
	local direction = room.info[randomRoom.Name]["Direction"]
	local hasStairs = room.info[randomRoom.Name]["HasStairs"]
	local hasEntity = room.info[randomRoom.Name]["HasEntity"]
	local prevHadEntity = room.info[prevRoom.Name]["HasEntity"]
	local prevHadStairs = room.info[prevRoom.Name]["HasStairs"]
	
	if (prevRoom.Name == randomRoom.Name) 
		or (hasEntity and prevHadEntity and prevHadEntity == hasEntity)
		or (direction and room.lastTurnDirection == direction)
	then
		return room.GetRandom(prevRoom)
	else
		if direction then
			room.lastTurnDirection = direction
		end
		
		return randomRoom
	end
end

function room.Special(prevRoom, SpecialRoom)
	local randomRoom = SpecialRoom
	local newRoom = randomRoom:Clone()

	newRoom.PrimaryPart = newRoom.EntranceBrick
	newRoom:PivotTo(prevRoom.ExitBrick.CFrame)

	newRoom.Parent = workspace.GeneratedRooms

	return newRoom
end

function room.Generate(prevRoom, roomNum)
	local randomRoom = room.GetRandom(prevRoom)
	local newRoom = randomRoom:Clone()
	
	newRoom.PrimaryPart = newRoom.EntranceBrick
	newRoom:PivotTo(prevRoom.ExitBrick.CFrame)
	
	newRoom.Parent = workspace.GeneratedRooms
	
	return newRoom
end

return room

Any help is appreciated!

1 Like
local count = 0
for i = 1, 100 do  -- Heres the for loop.
        count += 1
	prevRoom = room.Generate(prevRoom) -- Calling the generate room function from the module script.
end

count is now a script global counting number of loops. Just make sure to set it back to 0 when you do it again.

There is an easier way to do that, if my understanding about your problem is correct. You can create a table saying how each room should be based on its room number.

Then you use for loop with 3 starting parameters, then based on its number, you call a function that generate that type of room

Basically, this will create a special room if the number is in the table, if not, just create a common room.

--Table for special room.
local specialRoomList = {
  [2] = "Large",
  [5] = "Special",
  [8] = "Bigger",
  [9] = "Dark",
}

-- a function that create room based on string value
local function generateRoomList(RoomType)
  if RoomType == "Large" then
    print("Do Large room")
  elseif RoomType == "Special" then
    print("Do Special room")
  elseif RoomType == "Bigger" then
    print("Do Bigger room")
  elseif RoomType == "Dark" then
    print("Do Dark room")
  elseif RoomType == "Common" then
    print("Do Common room")
  end
end

-- for loop with 3 starting values.
for i = 1, 10, 1 do
  if specialRoomList[i] ~= nil then
    generateRoomList(specialRoomList[i])
  else
    generateRoomList("Common")
  end
end
--OUTPUT
Do Common room
Do Large room
Do Common room
Do Common room
Do Special room
Do Common room
Do Common room
Do Bigger room
Do Dark room
Do Common room
1 Like

I have already tried this and did not seem to work maybe someone else will have the solution. Thank you for your help!

1 Like

You can access the counter variable directly in the for loop.

for i = 1, 100 do
	print("room number:", i)
	prevRoom = room.Generate(prevRoom, i)
end

I took your code and edited it around a little and it worked! Thank all of you for helping!

Nice! I’m glad I could help. Enjoy coding!

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