I have been trying to use the
local players = game.Players:getChildren()
in my function , this is what i go.
if players == 1 then print("Starting Minigame.") print("Enough Players!") m = Instnce.new("Message", game.Workspace) m.Text = "Starting Minigame!" wait(5) m:Destroy() end
I get no errors in the output, but nothing happens. I've tried the NumPlayers variable but it does nothing.... Anyone mind pointing me in the right direction?
====================== I've also been trying to create a leaderboard stats that once the wait() is up, it gives them tools?
Leaderboard:
function CreateStats(name, parent, value) var = Instance.new("IntValue") var.Name = name var.Parent = parent var.Value = value end function Entered(newplayer) if newplayer:findFirstChild("leaderstats") == nil then CreateStats("leaderstats", newplayer, 0) CreateStats("Level", newplayer.leaderstats, 0) end end game.Players.PlayerAdded:connect(Entered) p = game.Players:GetChildren() for i = 1, #p do Entered(p[i]) end
The Timer:
if game.Players.LocalPlayer.leaderstats.Level.Value == 0 then wait(2) game.Players.LocalPlayer.leaderstats.Level = game.Players.LocalPlayer.leaderstats.Level.Value +1 wait(2) m = Instance.new("Message", game.Workspace) m.Text = "You've recieved build tools!" wait(2) m:Destroy() elseif game.Players.LocalPlayer.leaderstats.Level.Value == 1 then --Inserting random tools etc-- end
Thanks in advanced.
In your second bit of code, you made a simple mistake:
if players == 1 then
players
will never be equal to the integer 1, because players
is a reference to a Table. You have to use the size-of operator (a hash: #) to perform this check properly. Another thing to note is that this will only run if there is exactly one player playing, and I assume you want it to be one or more, so the equals has to be changed to greater than or equal.
The size-of operator for Lua Tables only works properly if the table has only integer indices that start at 1 and go up by 1 for every value in the table.
(This is basically the default, this table:
{element1, 21, "turbulance", sasquatch.Element.Value}
follows that pattern. Note how I let the elements be given indexes automatically.)
Any ROBLOX method that return a table, e.g. GetChildren
, will have the table be in this format.
if #players >= 1 then
Since you are using a snapshot of Players, which is a good practice in case somebody joins in the middle of a game, I would avoid using the NumPlayers property in this case.