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

Why isn't this Number Value right?

Asked by 9 years ago

I have a number value inside a TextLabel, and the TextLabel is supposed to tell the player how many people are on the server. i wasn't sure how to go about this so I tried this:

The script inside the Value:

plr = game.Players:GetPlayers()

game.Players.PlayerAdded:connect(function()
    script.Parent.Value = plr
    return #plr
end)

game.Players.PlayerRemoving:connect(function()
    script.Parent.Value = plr
    return #plr
end)

The Value is 0 in the PlayerGui

the script inside the TextLabel:

script.Parent.Text = "Players Online\n".. script.Parent:WaitForChild("Value").Value

script.Parent:WaitForChild("Value").Changed:connect(function()
    script.Parent.Text = "Players Online\n".. script.Parent.Value.Value
end)

There is nothing in output, and nothing in that F9 thing. The TextLabel says

Players Online 0

0
Use an IntValue instead! When you use a NumberValue, things can get really weird, really fast. Your 10 that you just saved to the NumberValue could be 10.0000007 or something like that. IntValues can only hold whole numbers. Validark 1580 — 9y
0
http://www.roblox.com/games/197480972/more-testing still didn't work. The thing is, there is no number in the number value, just a 0. Senor_Chung 210 — 9y

1 answer

Log in to vote
2
Answered by
Validark 1580 Snack Break Moderation Voter
9 years ago

There are two ways to find the number of players in a server.

The first:

--localscript inside StarterGui
local players=script.Parent.Parent.Parent:GetChildren() --make sure you have enough .Parent's to get a table of everything inside of Players
--script.Parent.Parent... ... until the script is looking at PlayerGui  then add .Parent.Parent to that.
--Stuff>PlayerGui>Player>Players

--#players is the number of players in the server

The second:

game.Players.NumPlayers --read-only property telling how many players per server

You don't need a value at all, and you also only need only 1 script

script.Parent.Text = "Players Online\n".. script.Parent.Parent.Parent.Parent.NumPlayers --you might have to adjust the amount of .Parent's

If you want it to change each time a player joins or leaves:

function Update()
script.Parent.Text = "Players Online\n".. script.Parent.Parent.Parent.Parent.NumPlayers --you might have to adjust the amount of .Parent's
end

script.Parent.Parent.Parent.Parent.ChildAdded:connect(Update)
script.Parent.Parent.Parent.Parent.ChildRemoved:connect(Update)
Ad

Answer this question