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

Why cant this message appear after the game found 2 players?

--Variables--
local plr = game:GetService('Players')
local sg = game:GetService('StarterGui')

--Functions--

function message(text,color)
	sg:SetCore("ChatMakeSystemMessage", {
		Text = text or error("Text is nil"),
		Color = color or Color3.fromRGB(225,225,225),
		Font = Enum.Font.SourceSans,
		FontSize = Enum.FontSize.Size24,
	})
end

function lookForPlayers()
	while true do wait()
	local playerCount = script.Parent:WaitForChild("playerCounter").existingPlayers.Value
	if playerCount >= 2 then
		print("There is enough players!")
		break
	else
		print("Not enough players!")
	end
	end
end



--Main--

lookForPlayers()

wait()

message("The round is about to start!", Color3.fromRGB(0,225,0)) -- Wont write the message in the server chat

1 comment

0

The player counter script works and it recognizes that there is two players in the game, but it wont write the message in the chat after enough players has joined the server

1 answer

Have you tried print debugging? Perhaps playerCount isn't updating properly. Somehow the loop is becoming endless.

Reguardless, you can use the GetPlayers function of Players to return a table that consists of all the current players in game. Use the # operator on this table to return the number of entries; i.e, the number of players.

And here is a cleaner way to go about waiting with the while loop:

function lookForPlayers()
	local plrs = #game.Players:GetPlayers() --Initially recieve player amount
	--Establish loop and conditions for iteration
	while plrs < 2 and wait() do
		print("Not enough players");
		plrs = #game.Players:GetPlayers() --Update val
	end
	print("There is enough players!");
end 

3 comments

0
1

You should use .PlayerAdded and .PlayerRemoving, so you check the amount less amount of times.

0

dis guy knows about optimization :3

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