Game not starting a server or taking very long to load upon joining

Hello once again!

So today i’ve been making a boss battle with a tutorial with various bugs. Which i’ve already asked how to solve and succesfully done so. But now I’ve run into an critical bug that could ruin the whole game. So in the boss there are currently 2 stages which include spawning and moving parts. though when I added the 2nd stage it started breaking the game and not loading me in. To the point where it would nto even start a server. Can somebody help me fix this? Here’s the code:

Stages Control code:

local firstStage = script.Parent:WaitForChild("FirstStage")
local secondStage = script.Parent:WaitForChild("SecondStage")

game.Players.PlayerAdded:Connect(function(player)
	local playerCount = game.Players:GetChildren()
	if #playerCount == 1 then
		wait(300)
		firstStage.Value = false
		secondStage.Value = true
	end
end)

Second Stage Script:

local secondStage = script.Parent.Parent:WaitForChild("SecondStage")
local rain = game.ServerStorage:WaitForChild("Rain")
local X = Instance.new("NumberValue")
local Z = Instance.new("NumberValue")
X.Value = 0
Z.Value = 0

while true do
	if secondStage.Value == true then
		X.Value = math.random(-84.572, 9.053)
		Z.Value = math.random(-23.954, 70.168)
		local rain2 = rain:Clone()
		rain2.Parent = game.Workspace
		rain2.Position = Vector3.new(X.Value, rain2.Position, Z.Value)
		wait(5)
	end
end

Your problem is probably because when secondStage is false, there’s no wait to yield the infinite loop, you can simply add a wait()

while true do
	if secondStage.Value == true then
		X.Value = math.random(-84.572, 9.053)
		Z.Value = math.random(-23.954, 70.168)
		local rain2 = rain:Clone()
		rain2.Parent = game.Workspace
		rain2.Position = Vector3.new(X.Value, rain2.Position, Z.Value)
		wait(5)
	end
    wait()
end

Or better yet in your case, just make the loop happen when the secondStage is true, which yuo can use via the Changed event of a baseValue! Or GetpropertyChangedSignal since the return isn’t cared for

secondStage:GetPropertyChangedSignal("Value"):Connect(function()
	while secondStage.Value do
		X.Value = math.random(-84.572, 9.053)
		Z.Value = math.random(-23.954, 70.168)
		local rain2 = rain:Clone()
		rain2.Parent = game.Workspace
		rain2.Position = Vector3.new(X.Value, rain2.Position, Z.Value)
		wait(5)
	end
end

Is there a 3rd option that still fixes it easily like the 2nd but doesn’t basically ruin the point of having math.random? since now it will spawn in the same place everytime ruining the point of having to constantly move.

Try printing out the Values of X and Z in the while loop after updating them and see what it prints, it could be that you’re not using whole numbers? Maybe try this?

secondStage:GetPropertyChangedSignal("Value"):Connect(function()
	while secondStage.Value do
		X.Value = math.random(-84, 9)
		Z.Value = math.random(-23, 70)
		local rain2 = rain:Clone()
		rain2.Parent = workspace
		rain2.Position = Vector3.new(X.Value, rain2.Position, Z.Value)
		wait(5)
	end
end

Alright, I’ll try that. while i was waiting for ur respone and tried option 1. which din’t fix it sadly. So i hope this will work.

the 1st option also broke the script for the 2nd stage.

Alright. So I am trying this option now and its still taking very long to load. So we might have to find another solution if there is any.

Wait very long to load meaning it takes a lot time for the 2nd stage to run or?

Like, when I join the game. It takes way longer then normal and sits on the roblox loading screen for quite a while before putting me in the game. and when running it on roblox it can’t even create a server for some reason.

I have a feeling it has something to do with the StageControl script or the first stge script.As the second stage script wont work.

It could be since I dont see anything else wrong with those scripts. That or there’s a lot of things needing to be created that’s causing a lot of lag

Here’s the first stage script:

local spike = game.ServerStorage:WaitForChild(“Spike”)
local danger = game.ServerStorage:WaitForChild(“Danger”)
local firstStage = script.Parent.Parent:WaitForChild(“FirstStage”)
local X = Instance.new(“NumberValue”)
local Z = Instance.new(“NumberValue”)

while true do
if firstStage.Value == true then
X.Value = math.random(-84.572, 9.053)
Z.Value = math.random(-23.954, 70.168)
local spike2 = spike:Clone()
local danger2 = danger:Clone()
danger2.Parent = game.Workspace
danger2.Position = Vector3.new(X.Value, danger2.Position.Y, Z.Value)
spike2.Position = Vector3.new(X.Value, spike2.Position.Y, Z.Value)
wait(1.5)
spike2.Parent = game.Workspace
danger2.Parent = nil
end
wait(0.5)
end

Also, just note that I did indeed duplicate these scripts to make it harder for the player. So I don’t know if thats the cause to make it take super long to load. but that doesn’t explain the fact that a pretty good ipad of mine if it tries to play it can’t even create a server.

That could maybe be related? What happens if you don’t duplicate them and just have 1 of each script?

So I’ve done that and it has indeed loaded way faster then originally. But how do I make sure it is still hard for the player now without breaking the game again? Also it is weird that the 1st stage if i duplicate it its fine but 2nd stage it breaks. whats up with that.

You can probably just decrease the wait time you have currently, and I recommend you do the same thing I had mentioned previously using GetPropertyChangedSignal so it only loops when it has to

Right now for stage 2 you have it so it waits 5 seconds, maybe lower it?

that only increases the speed it will spawn as. but doesn’t increase the amount drasticly. Unless I’d do like 0.1 which then we would probebly run into the same problem.

Should also probebly be noted that there is a bug called: Workspace.Rain.Fall:7: attempt to index nil with ‘Parent’ and also it doesnt delete the rain when it hits the max amount that it can go down.

You’d maybe have to increase the amount of ran that spawns per iteration. Also, that’s new, what’s the fall code

Fall code is the code that makes the rain actually go down and then break once it has reached the destination which is under the map. though for some reason it does not delete the part. Here’s the code for that:

while true do
if script.Parent.Parent == workspace then
repeat
script.Parent.Position = Vector3.new(script.Parent.Position.X, script.Parent.Position.Y - 1, script.Parent.Position.Z)
wait(0.01)
until script.Parent.Position.Y <= -69.715
rain.Parent = nil
break
end
end