I’ve created a round-based system for my game, but I have a huge map that sometimes refuses to exist, and doesn’t get cloned into the workspace. I don’t exactly know why. I’ve tried to add waits everywhere, but that still doesn’t solve the issue. Help?
repeat task.wait() until game:GetService("ContentProvider").RequestQueueSize == 0
local currentMapValue = game.Workspace:WaitForChild("CurrentMap")
print("gamehandler initalized")
local mapFolderz = game.Workspace:WaitForChild("MapFolder")
local mapMusic = game.Workspace:WaitForChild("MapMusic")
local mapMusic2 = game.Workspace:WaitForChild("IntermissionMusic")
local mapFolder = game.ServerStorage:WaitForChild("Maps")
local brickSignal = game.Workspace:WaitForChild("brickSignal")
local postRoundmusic = game.Workspace["Sweden Ambient (Minecraft)"]
local mapAmount = 0
local debugHint = Instance.new("Hint")
debugHint.Parent = game.Workspace
debugHint.Text = "debug:"
local debugMode = false
--local abc = mapMusic2:GetChildren()
--local def = math.random(1, #abc)
--local musicc = abc[def]
--musicc:Play()
task.wait(10)
while task.wait() do
if not debugMode then
local eventChance = math.random(1,5)
local currentTime = 120
local intermissionTime = 20
--local snatchTime = 5
--print("stack start")
local maps = mapFolder:GetChildren()
--task.wait(0.3)
local ran = Random.new():NextInteger(1,#maps)
local ranMap = maps[ran]:Clone()
print("map cloned")
task.wait(10) -- the map can take a while to load
print(mapAmount)
if #maps == 0 then
print("No maps available.")
task.wait(0.1)
continue
end
if not ranMap then
print("Selected map doesn't exist.")
task.wait(0.1)
continue
end
local spawnSpot = ranMap:WaitForChild("SpawnSpot")
--print("got map")
local music = mapMusic:GetChildren()
--task.wait(0.3)
local ran = Random.new():NextInteger(1,#music)
local selectedMusic = music[ran]
local music2 = mapMusic2:GetChildren()
local ran2 = Random.new():NextInteger(1,#music2)
local selectedIntermissionMusic = music2[ran2]
--print("got music")
local everyoneDead = false
selectedIntermissionMusic.Volume = 1
selectedIntermissionMusic:Play()
--if ranMap then
-- selectedMap = ranMap
--else
-- repeat task.wait() until ranMap ~= nil
-- selectedMap = ranMap
--end
ranMap.Parent = mapFolderz
currentMapValue.Value = ranMap.Name
print("Map has been selected, sending this information to the value right now, this is insane; clip that now.")
repeat task.wait() until game:GetService("ContentProvider").RequestQueueSize == 0
while task.wait(1) do
debugHint.Text = "Round starts in: "..intermissionTime
if intermissionTime <= 15 then
game.Workspace["button.wav"]:Play()
end
if intermissionTime <= 0 then
intermissionTime = 0
break
end
intermissionTime -= 1
end
--task.wait(20) --20 second initilazation before everybody gets teleported to the map G
print("main gameplay")
for i,v in pairs(game.Players:GetPlayers()) do
if v and v.Character and v.Character:FindFirstChild("HumanoidRootPart") and v.Character:FindFirstChild("Humanoid") then
if v.Character:FindFirstChild("Humanoid").Health > 0 then
v.Character.HumanoidRootPart.CFrame = spawnSpot.CFrame
v.Team = game.Teams.Playing
end
end
end
print("teleported everybody")
task.wait(5)
intermissionTime = 15
while task.wait(1) do
debugHint.Text = "Ready? "..intermissionTime
game.Workspace["button.wav"]:Play()
if intermissionTime <= 0 then
snatchTime = 0
game.Workspace["button.wav"]:Play()
debugHint.Text = "Ready? "..snatchTime
break
end
intermissionTime -= 1
end
game.Workspace.roundStart:Play()
if eventChance == 1 then
local eventType = math.random(1,1)
print("event!")
game.Workspace["eventSound"]:Play()
if eventType == 1 then
local eventName = "Melee Madness"
local eventDescription = "Everyone has been given a sword. You can't kill the bricks."
for i,v in pairs(game.Players:GetPlayers()) do
if v.Team == game.Teams.Playing then
local tool = game.ServerStorage.Sword:Clone()
tool.Parent = v.Backpack
game.Debris:AddItem(tool, 120)
end
end
print("Event | Brickbattle")
end
end
--task.wait(5)
print("round begin")
for i = 1,100,1 do
task.wait(0.01)
selectedIntermissionMusic.Volume -= 0.01
--musicc.Volume -= 0.01
end
print("l1")
--musicc:Stop()
selectedIntermissionMusic:Stop()
selectedMusic:Play()
selectedMusic.Volume = 0
print("l2")
task.wait(2)
brickSignal.Value = 1
for i = 1,100,1 do
task.wait(0.01)
selectedMusic.Volume += 0.01
end
print("l3")
print("music start, brick spawn?")
while task.wait(1) do
if #game.Teams.Playing:GetPlayers() <= 0 then
print("everyone is de-a--a-a-ad")
everyoneDead = true
break
end
debugHint.Text = "Time remaining: "..currentTime
if currentTime <= 15 then
game.Workspace["button.wav"]:Play()
end
if currentTime <= 0 then
print("Time's up.")
currentTime = 0
break
end
currentTime -= 1
end
brickSignal.Value = 0
game.Workspace.roundEnd:Play()
currentTime = 0
task.wait(3)
debugHint.Text = "Round is over."
-- 2 minutes of gameplay, before everybody that is alive gets sent back to the lobby
for i,v in pairs(game.Players:GetPlayers()) do
if v.Team == game.Teams.Playing then
v.Character.HumanoidRootPart.CFrame = game.Workspace.RespawnZone1.CFrame -- take them back to the lobbby before destorying the map
v.Team = game.Teams.Lobby
end
end
print("round end")
brickSignal.Value = 0
for i = 1,100,1 do
task.wait(0.01)
selectedMusic.Volume -= 0.01
end
print("l1")
selectedMusic:Stop()
postRoundmusic:Play()
postRoundmusic.Volume = 0
print("l2")
task.wait(2)
for i = 1,100,1 do
task.wait(0.01)
postRoundmusic.Volume += 0.01
end
everyoneDead = false -- reset
task.wait(5) -- wait for the players
mapFolderz:ClearAllChildren()
print("map cleared")
task.wait(15) -- let them rest before the round
for i = 1,100,1 do
task.wait(0.01)
postRoundmusic.Volume -= 0.01
end
else
debugHint.Text = "Round-system disabled for debugging"
end
--print("stack end")
-- go back
end
try setting the parent of the ranmap earlier in the code, where the ranmap is created. other parts of the code may be interfering with the map cloning process
also, check if streamingenabled is enabled, as this can (maybe) cause some problems with what ur doing if the map is too large.
you could also detect that if the map isn’t in the mapfolder after getting parented after maybe like 2 seconds, if it isn’t detected, clone it again until it is there. if this still doesn’t work, it could be due to other issues that you would have to diagnose
waitforchild() will yield forever if the map is not found, which is why “Infinite yield possible” is showing.
try parenting the cloned model to the mapfolder immediately after cloning it, otherwise the garbage collector (i think) will just discard it after a bit as it is parented to nil
I’ve tried these, now the script won’t even run anymore. The only thing it prints is “gamehandler intialized” which means that the top of the script ran, and the loop doesn’t run anymore.
repeat task.wait() until game:GetService("ContentProvider").RequestQueueSize == 0
local currentMapValue = game.Workspace:WaitForChild("CurrentMap")
print("gamehandler initalized")
local mapFolderz = game.Workspace:WaitForChild("MapFolder")
local mapMusic = game.Workspace:WaitForChild("MapMusic")
local mapMusic2 = game.Workspace:WaitForChild("IntermissionMusic")
local mapFolder = game.ServerStorage:WaitForChild("Maps")
local brickSignal = game.Workspace:WaitForChild("brickSignal")
local postRoundmusic = game.Workspace["Sweden Ambient (Minecraft)"]
local mapAmount = 0
local debugHint = Instance.new("Hint")
debugHint.Parent = game.Workspace
debugHint.Text = "debug:"
local debugMode = false
--local abc = mapMusic2:GetChildren()
--local def = math.random(1, #abc)
--local musicc = abc[def]
--musicc:Play()
task.wait(10)
while task.wait() do
if not debugMode then
local eventChance = math.random(1,5)
local currentTime = 120
local intermissionTime = 20
--local snatchTime = 5
--print("stack start")
local maps = mapFolder:GetChildren()
--task.wait(0.3)
local ran = Random.new():NextInteger(1,#maps)
local ranMap = nil
if #mapFolder:GetChildren() > 0 then
local ranMap = maps[ran]:Clone()
task.wait(2)
continue
end
ranMap.Parent = mapFolder
print("map cloned")
task.wait(10) -- the map can take a while to load
print(mapAmount)
if #maps == 0 then
print("No maps available.")
task.wait(0.1)
continue
end
if not ranMap then
print("Selected map doesn't exist.")
task.wait(0.1)
continue
end
--print("got map")
local music = mapMusic:GetChildren()
--task.wait(0.3)
local ran = Random.new():NextInteger(1,#music)
local selectedMusic = music[ran]
local music2 = mapMusic2:GetChildren()
local ran2 = Random.new():NextInteger(1,#music2)
local selectedIntermissionMusic = music2[ran2]
--print("got music")
local everyoneDead = false
selectedIntermissionMusic.Volume = 1
selectedIntermissionMusic:Play()
--if ranMap then
-- selectedMap = ranMap
--else
-- repeat task.wait() until ranMap ~= nil
-- selectedMap = ranMap
--end
--ranMap.Parent = mapFolderz
currentMapValue.Value = ranMap.Name
print("Map has been selected, sending this information to the value right now, this is insane; clip that now.")
local spawnSpot = ranMap.SpawnSpot
repeat task.wait() until game:GetService("ContentProvider").RequestQueueSize == 0
while task.wait(1) do
debugHint.Text = "Round starts in: "..intermissionTime
if intermissionTime <= 15 then
game.Workspace["button.wav"]:Play()
end
if intermissionTime <= 0 then
intermissionTime = 0
break
end
intermissionTime -= 1
end
--task.wait(20) --20 second initilazation before everybody gets teleported to the map G
print("main gameplay")
for i,v in pairs(game.Players:GetPlayers()) do
if v and v.Character and v.Character:FindFirstChild("HumanoidRootPart") and v.Character:FindFirstChild("Humanoid") then
if v.Character:FindFirstChild("Humanoid").Health > 0 then
v.Character.HumanoidRootPart.CFrame = spawnSpot.CFrame
v.Team = game.Teams.Playing
end
end
end
print("teleported everybody")
task.wait(5)
intermissionTime = 15
while task.wait(1) do
debugHint.Text = "Ready? "..intermissionTime
game.Workspace["button.wav"]:Play()
if intermissionTime <= 0 then
snatchTime = 0
game.Workspace["button.wav"]:Play()
debugHint.Text = "Ready? "..snatchTime
break
end
intermissionTime -= 1
end
game.Workspace.roundStart:Play()
if eventChance == 1 then
local eventType = math.random(1,1)
print("event!")
game.Workspace["eventSound"]:Play()
if eventType == 1 then
local eventName = "Melee Madness"
local eventDescription = "Everyone has been given a sword. You can't kill the bricks."
for i,v in pairs(game.Players:GetPlayers()) do
if v.Team == game.Teams.Playing then
local tool = game.ServerStorage.Sword:Clone()
tool.Parent = v.Backpack
game.Debris:AddItem(tool, 120)
end
end
print("Event | Brickbattle")
end
end
--task.wait(5)
print("round begin")
for i = 1,100,1 do
task.wait(0.01)
selectedIntermissionMusic.Volume -= 0.01
--musicc.Volume -= 0.01
end
print("l1")
--musicc:Stop()
selectedIntermissionMusic:Stop()
selectedMusic:Play()
selectedMusic.Volume = 0
print("l2")
task.wait(2)
brickSignal.Value = 1
for i = 1,100,1 do
task.wait(0.01)
selectedMusic.Volume += 0.01
end
print("l3")
print("music start, brick spawn?")
while task.wait(1) do
if #game.Teams.Playing:GetPlayers() <= 0 then
print("everyone is de-a--a-a-ad")
everyoneDead = true
break
end
debugHint.Text = "Time remaining: "..currentTime
if currentTime <= 15 then
game.Workspace["button.wav"]:Play()
end
if currentTime <= 0 then
print("Time's up.")
currentTime = 0
break
end
currentTime -= 1
end
brickSignal.Value = 0
game.Workspace.roundEnd:Play()
currentTime = 0
task.wait(3)
debugHint.Text = "Round is over."
-- 2 minutes of gameplay, before everybody that is alive gets sent back to the lobby
for i,v in pairs(game.Players:GetPlayers()) do
if v.Team == game.Teams.Playing then
v.Character.HumanoidRootPart.CFrame = game.Workspace.RespawnZone1.CFrame -- take them back to the lobbby before destorying the map
v.Team = game.Teams.Lobby
end
end
print("round end")
brickSignal.Value = 0
for i = 1,100,1 do
task.wait(0.01)
selectedMusic.Volume -= 0.01
end
print("l1")
selectedMusic:Stop()
postRoundmusic:Play()
postRoundmusic.Volume = 0
print("l2")
task.wait(2)
for i = 1,100,1 do
task.wait(0.01)
postRoundmusic.Volume += 0.01
end
everyoneDead = false -- reset
task.wait(5) -- wait for the players
mapFolderz:ClearAllChildren()
print("map cleared")
task.wait(15) -- let them rest before the round
for i = 1,100,1 do
task.wait(0.01)
postRoundmusic.Volume -= 0.01
end
else
debugHint.Text = "Round-system disabled for debugging"
end
--print("stack end")
-- go back
end
You have defined ranMap twice as a local variable.
Try defining it once and parenting it in the same statement:
local maps = mapFolder:GetChildren()
local ran = Random.new():NextInteger(1,#maps)
local ranMap = nil
if #mapFolder:GetChildren() > 0 then
ranMap = maps[ran]:Clone()
ranMap.Parent = mapFolder
end
I do not see a local in front of ranMap = maps[ran]:Clone()
Where did you define ranMap as a local variable?
Not sure why you need the continue as any script will do that without being to to do so.
Try this block of code:
local maps = mapFolder:GetChildren()
local ran = Random.new():NextInteger(1,#maps)
local ranMap = nil
if #mapFolder:GetChildren() > 0 then
ranMap = maps[ran]:Clone()
ranMap.Parent = mapFolder
end
Wait, it seems like after I reverted my code to a previous state, it magically started working again? I still think that there is a chance that it loads slowly because of server lag, so is there any way to detect when the map is loaded first before I do anything else?
The only way to know for sure would be to get the number of descendants of the map on ServerStorage then check the cloned map to see if the count matches.