I have a fully functioning
roundSystem script which handles many key features of my game, including CFrames, Incrementing Of Values, etc. However, I’m not satisified fully with its state now, as it’s had a very bumpy past, constantly throwing errors, acting slow etc. One major factor, I believe, is the cleanup function.
I’ve had to clone/destroy models with hundreds of parts, in attempt to refresh the game once the round has occurred. This has lead to many
script exhaustion time errors, and it’s disrupted gameplay heavily.
I managed to subdue the constant errors by introducing
Runservice.Heartbeat:Wait and a lot of
Waits but I still wonder if anything more can be done to optimize this (making it as fast as possible), to prevent long delays between rounds (Like I am experiencing at the moment.
function CleanUp() workspace.Current.Part.SurfaceGui.TextLabel.Text = "Regenerating Map For Next Round" for i, v in pairs(workspace.SpleefMap:GetDescendants()) do if v:IsA("Part") then v:Destroy() game:GetService('RunService').Heartbeat:Wait() end end for i, v in pairs(workspace:GetChildren()) do if v.Name == "Portal" or v.Name =="UFO" or v.Name == "JumpPad" then v:Destroy() game:GetService('RunService').Heartbeat:Wait() end end local Clone = game.ServerStorage.SpleefMap:Clone() wait(1) game.ReplicatedStorage.GeneratingMap:FireAllClients(true) wait(8) print("Regenerating") game.ReplicatedStorage.Regenerating.Value = true Clone.Parent = workspace local chosenColor = BrickColor.Random() for i, v in pairs(Clone:GetDescendants()) do if v:IsA("Part") then v.BrickColor = chosenColor game:GetService('RunService').Heartbeat:Wait() end end wait(1) local ss = game.ServerStorage.PartTagger:Clone() ss.Parent = Clone wait() game.ReplicatedStorage.Regenerating.Value = false workspace.Current.Part.SurfaceGui.TextLabel.Text = "" end
Waits in there are annoying for me, and I want to find a way to remove as many of them as possible, as these are preventing the errors, but they are provoking more time to wait, which is makes the game more tedious. Thanks!