Hello, for the past few days I’ve been trying to create a level randomization system that starts at a level called “level start”, chooses random levels from a folder in replicated storage and moves them into the workspace, also aligning themselves with the previous level that was placed.
The system works by having a dictionary with the object and 2 other variables that let the system only randomly choose individual levels a certain amount of times.
I essentially use
getObject()
to choose a random level from the “Levels” dictionary by creating an array to allow the math.random to find a random level (since it doesnt work with dictionaries due to random only finding numerical values)
I also use a function called
getOldObject
which creates an array that stores the object of each previously randomly chosen level, and if there isnt a previous level already in that array, it returns the start level as the “old_Object” variable.
What is the issue?
my code works at first glance but has some major issues. To start off, the levels don’t actually get placed where the last level’s (“End_Point” ) CFrame is.
Along with that, spaces will be left between levels to show that the system isn’t working and isn’t actually placing each level in order (despite the output printing both the previous object and the current object successfully… i think)
Finally, as seen above in the image aswell, not all levels will be always chosen, appearing as if the script just stops itself prematurely for no reason.
Module_Script code: (with functions)
local module = {}
local repStorage = game:GetService("ReplicatedStorage")
local level_Storage = repStorage:FindFirstChild("Level_Storage")
local level_Workspace = game.Workspace:FindFirstChild("World").Levels
local startPoint = level_Workspace:FindFirstChild("LevelStart")
local Levels = {
["Level1"] = {
Object = level_Storage:FindFirstChild("Level1"),
allowedUse = 1,
Used = 0
},
["Level2"] = {
Object = level_Storage:FindFirstChild("Level2"),
allowedUse = 1,
Used = 0
},
["Level3"] = {
Object = level_Storage:FindFirstChild("Level3"),
allowedUse = 1,
Used = 0
},
["Level4"] = {
Object = level_Storage:FindFirstChild("Level4"),
allowedUse = 1,
Used = 0
}
}
local levelsArr = {}
for i, _ in pairs(Levels) do
table.insert(levelsArr, i)
end
local previousLevels = {}
function module.getOldObject(random_Object) --returns the previous object selected from getObject()
table.insert(previousLevels, random_Object)
local old_Object = previousLevels[#previousLevels - 1]
if old_Object then
return old_Object
else
old_Object = startPoint
print(old_Object)
return old_Object
end
end
function module.getObject() --returns the current randomly selected Object
local getRandom = levelsArr[math.random(1, #levelsArr)]
local random_Object = Levels[getRandom]
if not random_Object.Object then return end
if random_Object.Used < random_Object.allowedUse then
module.getOldObject(random_Object)
random_Object.Used = random_Object.Used + 1
return random_Object
end
end
function module.moveObject()
local currentObject = module.getObject()
if not currentObject then return end
local oldObject = module.getOldObject()
local oldObjectCFrame = oldObject.Object.End_Point.Position
if not oldObject then return end
if oldObject == startPoint then
currentObject:Clone()
currentObject.Parent = level_Workspace
oldObjectCFrame = oldObject.End_Point.Position
currentObject.Object:SetPrimaryPartCFrame(CFrame.new(oldObjectCFrame))
else
currentObject.Object:Clone()
currentObject.Object.Parent = level_Workspace
oldObjectCFrame = oldObject.Object.End_Point.Position
currentObject.Object:SetPrimaryPartCFrame(CFrame.new(oldObjectCFrame))
end
end
return module
Script code: (that runs the functions)
local serverScriptService = game:GetService("ServerScriptService")
local main_module = serverScriptService.Level_System:FindFirstChild("Main_L")
local module = require(main_module)
local ran = false
local function runObject()
if ran == false then
module.getObject()
wait(1)
module.moveObject()
ran = true
else
ran = false
return
end
end
while task.wait() do
runObject()
end
Output:
The “Old Object:” should return the Start_Level, not the first level chosen, and not all levels were ran even though they were supposed to.
I have looked for solutions on the forums, but nothing really applies to my code.