Help with not generating gems if a team has 0 players

Hi, I am working on a game called gemhunter for my game spacewars, and I need to make it so if a team has no players, no gems are generated.
Here is the code. Right now, gems are always generated for some reason

while true do
	local clone = coin:Clone()
	clone.Parent = game.Workspace
	local pos1 = math.random(605,655)
	local pos2 = math.random(-132, -72)
	clone.Position = Vector3.new(pos1,41,pos2)
	wait(2)	
end

Alright, please help it actually did NOT work. It never spawns gems.
Code:

local coin = game.ServerStorage.BlueDiamond -- Path to coin
local teams = game:GetService("Teams"):GetTeams()
local blueTeam = nil
for _, team in pairs(teams)do
	if team.Name == "Blue" then
		blueTeam = team
	end
end
local players = blueTeam:GetPlayers()
print(#players)
while true do
	wait(0.1)
	if #players ~= 0 then
		
		local clone = coin:Clone()
		clone.Parent = game.Workspace
		local pos1 = math.random(605,655)
		local pos2 = math.random(-132, -72)
		clone.Position = Vector3.new(pos1,41,pos2)
		wait(1.9)	
	end
end

local coin = game.ServerStorage.BlueDiamond -- Path to coin
local teams = game:GetService("Teams"):GetTeams()
local blueTeam = nil
for _, team in pairs(teams)do
	if team.Name == "Blue" then
		blueTeam = team
	end
end
while wait(0.1) do -- I put the wait here for efficiency
    local players = blueTeam:GetPlayers() --move this line here, since you are only checking the number of players once and not every 0.1 seconds
    print(#players)

	if #players ~= 0 then
		
		local clone = coin:Clone()
		clone.Parent = game.Workspace
		local pos1 = math.random(605,655)
		local pos2 = math.random(-132, -72)
		clone.Position = Vector3.new(pos1,41,pos2)
		wait(1.9)	
	end
end


1 Like