local ReplicatedStorage = game:GetService("ReplicatedStorage")
local modelsStage1 = ReplicatedStorage:WaitForChild("Stage1")
local modelsStage2 = ReplicatedStorage:WaitForChild("Stage2")
--First Stage
local function chooseModelStage1()
local models = modelsStage1:GetChildren()
if #models == 0 then
warn("Error1")
return nil
end
local randomIndex = math.random(1, #models)
return models[randomIndex]
end
local stage1 = chooseModelStage1()
if stage1 then
local previousCoords = game.Workspace.StartingSpot.EndHelper:GetPivot()
local X, Y, Z = previousCoords.Position.X, previousCoords.Position.Y, previousCoords.Position.Z
X = X -1
Z = Z -1.5
local newCoordinates = CFrame.new(X, Y, Z)
local clone = stage1:Clone()
clone.Parent = game.Workspace
clone:SetPrimaryPartCFrame(newCoordinates)
end
and the Replicated Storage is here
Why the code always choose Stage1Left?
When you use GetChildren however it returns a list of the object receiving that function, in this case a folder, which you can use #models to get the numerical value of the objects present in the “list”. I do not see the issue with the code right now, but it could also be luck?
local function chooseRandomModel(folderName)
local models = game:GetService("ReplicatedStorage").[folderName]:GetChildren()
local randomIndex = math.random(#models)
return models[randomIndex]
end
local randomModel = chooseRandomModel("Stage1")
if randomModel then
-- Rest of your code using the randomModel
end
why:
the chooseModelStage1 function only iterates through the child models of “Stage1” once.
This could actually be the issue, the game is still loading in models and since Stage1Left is the first of the Stage1 folder (meaning it might be prioritized to load first) so it might only count 1 value, since technically you’re waiting for the “folder” object to load not the children of the folder to, but it depends if this script is immediately run through or is activated by some action or at a specified time. For this reason, maybe I would try to wait for the game to load perhaps maybe delaying the scripts functions or just typically wait for all the objects of the game to fully load, an example would be to use waitForChild, but based on how you’re going about getting the number of values in the folder this might not be optimal unless you have a definite value to your folder. Only thing I can suggest is to delay the script activation time somehow by using task.wait(), wait() or other means such as using run service and waiting for like 60 frames to pass and then disconnecting that function which then would run the following code as a function after the disconnect, etc…
Well it depends on if the values of the folder going to be the same indefinitely or new values or values might become removed from time to time, as that might not be the optimal case, I would just make the script run fully have like 1-5 seconds or count frames using run service if so you are going to add or remove values so you don’t have to wait for every individual object to load in.