While loop is ignoring the condition needed for it stop the loop

Yes, it is on a normal script on the workspace. Not localscript, not on StarterPlayer.

Would you mind sharing the script’s content that involves manipulation of the player value and can you also run a print statement that shows the player value being updated in the while loop?

This script is in a LocalScript but it changes a value on a normal Script. It is on a local as it needs to check if the player died/left while the game was on

--Function that runs whenever a player leaves
game.Players.PlayerRemoving:Connect(function()
	--If the player that left is playing then...
	if isPlaying.Value == true then
		--Remove 1 from the player count
		workspace.Main.playerCount.Value = workspace.Main.playerCount.Value - 1
	end
end)
--Function that runs whenever a player dies
player.Died:Connect(function()
	--If the player that died is playing then...
	if isPlaying.Value == true then
		--Remove 1 from the player count
		workspace.Main.playerCount.Value = workspace.Main.playerCount.Value - 1
		print("Player died, removing 1 from player count, new value: "..workspace.Main.playerCount.Value)
	end
end)

This other one is an other local script and it does the same thing except it only substracts if the player won

local function onTouched(otherpart)
	--If the object that was touched is called "Win" then...
	if otherpart.Name == "Win" then
		--Teleport player to the win location
		player.HumanoidRootPart.CFrame = winLocation
		--Removes 1 player from the player count
		workspace.Main.playerCount.Value = workspace.Main.playerCount.Value - 1
		--Sets the player's status to not playing
		script.Parent.WaterScript.isPlaying.Value = false

Those two are the only local scripts that the game has so far.

This script is the one responsible for getting all the players in the server and storing them on totalCount, which will then be used to make playerCount. This script is not local

--Variables
local matchInProgress = script.matchInProgress
local Players = game:GetService("Players")
local totalCount = script.totalCount

--Function that runs when a player joins
Players.PlayerAdded:Connect(function()
	--When a player joins, 1 is added to the totalcount
	totalCount.Value = totalCount.Value + 1
end)
--Function that runs when a player leaves
Players.PlayerRemoving:Connect(function()
	--When a player joins, 1 is removed from the totalcount
	totalCount.Value = totalCount.Value - 1
end)

And finally, this is the function inside the main script that runs all of the timer stuff. It’s the only function in the script that uses those variables.

function gameTimer()
	playerCount.Value = MatchStats.totalCount.Value
	--Enable matchInProgress so no one else can join
	MatchStats.matchInProgress.Value=true
	--Variable that stores the time that has passed.
	local timePassed = 0
	--Sets the status value.
	status.Value = "Escape the rooms before the timer runs out!"
	--Repeat until the ammount of time that has passed is less than the time limit.
	
	while timePassed <= TimeLimit and playerCount.Value > 0 do
			--Time left is equal to itself minus the time that has passed.
			local TimeLeft = TimeLimit-timePassed
			--Sets the status value with the remaining time.
			status.Value = "You have "..TimeLeft.." seconds left to escape!"
			--Add 1 more second to the time that has passed.
			timePassed = timePassed+1
			wait(1)
			if playerCount.Value < 0 then
				print("Under 0, Value is: "..playerCount.Value)
			end
			if playerCount.Value > 0 then
				print("Above 0, Value is: "..playerCount.Value)
			end
			if playerCount.Value == 0 then
				print("Equal to 0, Value is: "..playerCount.Value)
			end
	end
end

This is how the scripts look in the explorer, with the variables that interact with others in some way.

And finally, this is the console, which is printing the value of playerCount. As you can see, before the player died, its value is 1, as expected, once it dies, it prints the new value, which is 0, just like expected, but when it prints the value again, instead of saying "Equal to 0, value is :0, it says 1

Hope this helps. Sorry for the long texts and scripts, I like to document my code a lot so I know what’s happening

This is your issue. For LocalScripts to interact with Server Scripts, networking must be used. Please consult Remote Events and Remote Functions for that information.

1 Like

Reading on it right now, it looks promising so far. I’ll try it out and if it works then thank you so much.

It worked like a charm!
Thank you so much.
I’ll triple check every local script and do the same whenever it is necesarry.