I am working on a game called Napoleon’s Campers with a small group of people. I’m still fairly new to scripting Lua.
In the game there is a round system that loads a new map from ServerStorage by using :clone() on a folder. The maps have a lot of parts in them so sometimes the round starts before it’s fully loaded in. Is there a function to get the progress of the current :clone()?
Round code
math.randomseed(tick())
local gameLength = 300
local intermissionLength = 15
local inround = game.ReplicatedStorage.InRound
local status = game.ReplicatedStorage.Status
local maps = {game.ServerStorage.RiverMap}
local selectedMap = "RiverMap"
local function loadMap(map)
print("loading map: "..map.Name)
selectedMap = map.Name
local newMap = map:Clone() -- This is where it clones; How do I get the progress of the running function?
newMap.Parent = game.Workspace
for i,player in pairs(game.Players:GetChildren()) do
game.Workspace:WaitForChild(player.Name).Humanoid.Health = 0
end
end
local function deleteMap()
print("deleting map")
local map = game.Workspace:WaitForChild(selectedMap)
map:Destroy()
end
inround.Changed:Connect(function()
if inround.Value == true then
local randy = math.floor(math.random(1, #maps))
loadMap(maps[randy])
end
if inround.Value == false then
deleteMap()
end
end)
local function runTimer()
while wait() do
for i = intermissionLength, 0, -1 do
status.Value = "Intermission: "..i.." seconds"
wait(1)
end
inround.Value = true
for i = gameLength, 0, -1 do
status.Value = "In-game: "..i.." seconds"
wait(1)
end
inround.Value = false
end
end
spawn(runTimer)
If some of the code seems similar, that’s because it’s partially based off a round system tutorial on YT. Like I said, I very recently started coding Lua.
you could count how much things there are inside of that map and then use :GetDescantes (i think i didn’t spell it correctly but you know the thing were im talking about) and then doing math with how much descantes there are already?
Highly recommend you don’t clone the entire object into workspace at once - you’ll likely kill some users e.g. mobiles. The solution to this is loading chunks of each map into workspace, which will also solve your current problem by yielding the thread until completed e.g.
Set up a folder/model with smaller folders/models inside that make up chunks of the map
Something like this
local function loadMap(mapname)
local map = game:GetService('ServerStorage').Maps[mapname]
local cloned = Instance.new('Model', game.Workspace)
cloned.Name = mapname
for i, v in ipairs(map:GetChildren() do
v:clone().Parent = cloned
-- either add a wait() here or do something like
if math.random() > 0.5 then -- or i % 3 == 0 then wait() etc
wait()
end
end
return cloned
end
local function getRand()
local maps = game:GetService('ServerStorage').Maps:GetChildren()
return maps[math.random(1, #maps)].Name
end
local function runTimer()
while wait() do
for i = intermissionLength, 0, -1 do
status.Value = "Intermission: "..i.." seconds"
wait(1)
end
inround.Value = true
loadMap(getRand()) -- will yield this thread until loadMap resolves
for i = gameLength, 0, -1 do
status.Value = "In-game: "..i.." seconds"
wait(1)
end
inround.Value = false
end
end