Error when using if x <= y then in a spawn(function()

When I try to check if a local is less than another local in a spawn(function(), I simply get an error. Here is the script code I am using:

local remotewin = ReplicatedStorage:WaitForChild("Remotewin")

local ingame = {}

local clasifmin = 0

local plrwinsnumber = 0

for i,player in pairs(game.Players:GetPlayers()) do
	table.insert(ingame, player)
end

clasifmin = math.round(#ingame*50 / 100)

spawn(function() -- start new thread
	for i,player in pairs(game.Players:GetPlayers()) do
		local function onCreatePart()
			if plrwinsnumber <= clasifmin then
			plrwinsnumber += 1
			end
			remotewin.OnServerEvent:Connect(onCreatePart)	
		end
	end
end)

while wait() do
	print ((plrwinsnumber).. "/" .. (clasifmin))
end

Not defined. Other than that, doesn’t seem to be any errors for me.

in the script I have these other locals but have not shown them

local players = game:GetService(“Players”)

local ReplicatedStorage = game:GetService(“ReplicatedStorage”)

local remotewin = ReplicatedStorage:WaitForChild(“Remotewin”)

What error do you get?

the error I get is that the server does not check that one number is less than another, and therefore does not print. But if I remove the line code

if plrwinsnumber <= clasifmin then

if printed, but the numbers that are printed are uncontrolled because there is no longer anything to check when to stop.

Updated code, not sure why you’d want a new thread for that, but here’s the updated version…

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remotewin = ReplicatedStorage:WaitForChild("Remotewin")

local ingame = {}

local clasifmin = 0

local plrwinsnumber = 0

clasifmin = math.round(#Players:GetPlayers()*50 / 100)

coroutine.wrap(function()
	while wait() do
		print ((plrwinsnumber).. "/" .. (clasifmin))
	end
end)()

Players.PlayerAdded:Connect(function(Player)
	clasifmin = math.round(#Players:GetPlayers()*50 / 100)
	local function onCreatePart()
		if plrwinsnumber <= clasifmin then
			plrwinsnumber += 1
		end
	end
	remotewin.OnServerEvent:Connect(function()
		onCreatePart()
	end)
end)

If this doesn’t function the way you wish, feel free to reply.

the script fails, but thanks for helping me anyway

Want to elaborate or… nah?

Now, the script starts printing 0/1, and seconds later it prints 2/1. And when the remote event calls it, the script does nothing.

Well what do you want the code to do?

I want to make that, for example, if in a game there are 4 players, the script prints 0/2 ((plrwinsnumber)… “/” ..(classifmin)) because it is 50% of the number of players. And, for each player that activates the remote event, add +1 to the local plrwinsnumber. And so, when the script checks that (plrwinsnumber) == (clasifmin) it will stop printing. And the last thing the server has to print is 2/2

In that case, try this. Make sure it is a server script in ServerScriptService.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remotewin = ReplicatedStorage:WaitForChild("Remotewin")

local ingame = {}
local clasifmin = 0
local plrwinsnumber = 0

local function onCreatePart()
	if plrwinsnumber <= clasifmin then
		plrwinsnumber += 1
	end
end

coroutine.wrap(function()
	while task.wait() do
        if plrwinsnumber == clasifmin then
            break
        end
		print ((plrwinsnumber).. "/" .. (clasifmin))
	end
end)()

remotewin.OnServerEvent:Connect(function(Player)
    onCreatePart()
end)

Players.PlayerAdded:Connect(function(Player)
	clasifmin = math.round(#Players:GetPlayers() / 2)
end)

now the server does not print anything