Return isn't working?

So, I’m making a game and I have a function that contains all of the game’s framework. I want to check if there aren’t any players left and return the function. Problem is that it’s not returning and ignoring the “if players <= 0 then return end” statement completely. Why is this happening and is there a solution to fix it?

Can we see you code? Also is there any function or event that you put the return inside of that are inside the function?

When there aren’t any players left in the game the server shutdowns, meaning it wont run that code I believe. Or it oculd be the code you’re using

players should be a Number Variable using #game.Players:GetPlayers() if you’re not using that

I can basically simplify my code.

function gameFramework()
  local map = game.ReplicatedStorage.Map:Clone()
  map.Parent = workspace
  
  local playersAlive = game.Players:GetPlayers()
  spawn(function()
		while map ~= nil and wait()  do
			for ind, v in pairs(playersAlive) do
				local char = v.Character
				char:WaitForChild("Humanoid").Died:Connect(function()
					if table.find(playersAlive, v) then
						print("Died")
						table.remove(playersAlive, ind)
					end	
				end)
			end
		end
	end)
   while wait() do
     if #playersAlive <= 0 then
       return
     end
end
end
wait(10)
gameFramework()

The code is also still running. Even the code after the I tell the code to return :confused:

I quickly fround a problem…

function gameFramework()
	local map = game.ReplicatedStorage.Map:Clone()
	map.Parent = workspace

	local playersAlive = game.Players:GetPlayers()
	spawn(function()
		while map ~= nil and wait()  do
			for ind, v in pairs(playersAlive) do
				local char = v.Character
				char:WaitForChild("Humanoid").Died:Connect(function()
					if table.find(playersAlive, v) then
						print("Died")
						table.remove(playersAlive, ind)
					end	
				end)
			end
		end
	end)
	while wait() do
		if #playersAlive <= 0 then
			return
		end
	end
end
wait(10)
gameFramework()

You cannot run any code under the function if theres a while true do. Same as this:

function test()
	while wait() do
      print("ssss")
	end
end

test()
wait(1)
print("Nah nah")

Tell me if theres any problems.

You only need to check how many players are left when a player leaves or dies. Both which have events that can be used. Ideally you would like to get rid of any loops if possible.

local function getPlayerCount()
    return #playersAlive
end

game.Players.PlayerRemoving:Connect(getPlayerCount)
--Make your other event connection here for whenever a player dies, however you handle it.

I just sort of rewrote my code into that function. They’re basically the same though.

I do have that in my code. The code above is basically my code but rewritten.

Try using:

function hi()
spawn(function()
	while wait(1) do
		print("hi")
	end
end)
end
print("Now, it works. By using spawn, it will work :D")

hi()

Still no.
image
^ This is from my actual code btw…

This loop will keep running, even if the players are equal or less than 0

Can’t you just use a break statement to break the loop instead…?

while wait() do
    if #playersAlive <= 0 then
       break
    end
end

That would just break the loop, not return the whole function.

If you’re using the normal player thing you need to do #players.