-
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.
-
What is the issue?
I cannot figure out how and i have tested many solutions and none of them seem to work. -
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!