Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 10 years ago

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

Log in to vote
0
Answered by
Relatch 550 Moderation Voter
10 years ago
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
Ad
Log in to vote
2
Answered by
Merely 2122 Moderation Voter Community Moderator
10 years ago

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)
Log in to vote
-1
Answered by 10 years ago

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

Answer this question