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

What is wrong with my script to change game mode on >= 3 players?

plr = game.Players.NumPlayers

while true do wait(.2)

if plr < 3 then
	script.Parent.TextColor = BrickColor.new("Really red")
	script.Parent.Text = ("False")
	
else script.Parent.TextColor = BrickColor.new("Bright green")
	script.Parent.Text = ("True")
end

end

-Issue- When the game has hit >= 3 players, the game mode shows "True" only on the 3rd player. On the other players it shows false. Please help!

3 answers

✓ Accepted answer
if #game.Players:GetPlayers() > 3
    script.Parent.TextColor = BrickColor.new("Really red")
    script.Parent.Text = ("False")
else script.Parent.TextColor = BrickColor.new("Bright green")
    script.Parent.Text = ("True")
end

You set the plr variable to the value of game.Players.NumPlayer. It's never going to change because it's just a copy of the number. In your while loop, you should be using game.Players.NumPlayer instead of plr.

However, there's an entirely better way to do this. Instead of polling every 0.2 seconds, you can just update the text whenever a player is added or removed from the Players service.

local Players = game:GetService("Players")

function updateText()
	if Players.NumPlayers < 3 then
		script.Parent.TextColor = BrickColor.new("Really red")
	    	script.Parent.Text = ("False")
	else
		script.Parent.TextColor = BrickColor.new("Bright green")
		script.Parent.Text = ("True")
	end
end

updateText()
Players.ChildAdded:connect(updateText)
Players.ChildRemoved:connect(updateText)

Also, to get the number of something, you generally need to put a hash tag before the value, like this.

local Players = game.Players
if #Players < 3 then
-- CODE HERE
end

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