Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why doesn't the while wait() break?

I am trying to make my first ever FE compatiable game, and this script below is just to check if there is atleast 2 players in the game, if there is then it should FireClient to the LoadGame RemoteFunction, but it doesn't even print "test?" above it, why?

local directory = game:GetService("ServerScriptService")

local events = game:GetService("ReplicatedStorage")
--
local event = events.Remotes.ChangeText
local playervalue = directory.Values.Online
	game.Players.PlayerAdded:connect(function()
		wait(1)
		playervalue.Value = playervalue.Value - 1
		print(playervalue.Value)
	end)
while wait(2) do -- Checks to see if there are more than 2 players in the game
	if playervalue.Value <= 2 then
		
		event:FireAllClients(playervalue.Value, "StatusBar")
	elseif playervalue.Value >= 2 then
			break
		end
end
print("test?")
for i=1, 15, 0 do
	i=i - 1
	local loadgame = events.Remotes.LoadGame
	loadgame:FireAllClients(i)	
end

1 answer

✓ Accepted answer

Problem

Your while loop translate to break when value is more than two or is two, but if value is less than two or is two then fire all clients. Since the first statement fires first it doesn't check for the other one. Your loop will break when value is three.

Solution

You can fix this by just checking if the value is less than two and leave it as that.

while playervalue.Value < 2 do
wait(2);
event:FireAllClients(playervalue.Value, "StatusBar");
end

1 comment

0

Alright, I replaced it, but it still won't print "test?" after 2 players are in the game.

This discussion is preserved as it was. New answers, comments, and votes are closed.