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

How To Change Everyone's Stats?

How would I make it so that it would change everyone's stats at once?

For instance this is what I thought would work:

local gwar = game.Players:GetChildren() gwar.leaderstats.Level.Value = 150 end

3 answers

Introduction

This particular problem will require you to know a bit about tables and how to manipulate them. Here is a link to the Tables section of the official Lua programming guide. Now that that's said, let's get started.


GetChildren() returns a table, or "list", containing the direct children of an object.

Table elements are also called "key-value" pairs, because for each element there is a "key", which serves as a position in the table, and a "value".

Without going into the details right now, I'd like to recommend using Players:GetPlayers(), which returns a table containing all the players connected to the game, instead of GetChildren().

You can get the table of all connected players like so:

local children = game.Players:GetPlayers()

Now that you have a reference to the table of players, you can "browse" or "iterate" through the elements of the table through the pairs standard Lua function:

for i, v in pairs(children) do
	v.leaderstats.Level.Value = 150
end

The tables returned by GetChildren() and GetPlayers are formatted like so:

{
    [0] = Player1,
    [1] = SourceOfLightning
}

where the part in bracket is the index (or key) and the part on the right of the equal sign is its associated value.

In a generic for loop using the standard Lua pairs function,

  • the first variable (which I named i) is given the index of a table element,
  • while the second (which I named v) is given the value of associated to that index, in the table.

A generic for loop using the pairs iterator generator function will allow you to do some operations on each element of a table. In this case, set the Value property of all players' Level stat to 150.


Conclusion

I won't get into iterator generator functions (such as pairs) in details here, as this is a topic for a whole new tutorial, for now just keep in mind it is used in conjunction with generic for loops to browse through each element of a table.

To know more about why your original code doesn't work, please refer to the Common Mistakes blog article.

Anyway, I hope this helped. If it did then please be sure to accept, and optionally upvote, my answer. It helps both of us. ;) If you need anything else, you need only ask.

Loop through the players one at a time

There has been an article about this very problem. Read here to understand more about it.


To loop through all the players, we're going to use a generic for loop.

for _,plr in pairs(game.Players:GetPlayers()) do-- Generic For
	plr.leaderstats.Level.Value = 150-- Your code
end-- End

Warning,

If you do this at the start of a script, the script will almost certainly run before any players have actually joined the server. Add a wait, or some event to account for this.

Good Luck!

If I helped, please don't forget to click the accept button to accept my answer!

Try this

for _, player in pairs(game.Players:GetPlayers()) do
	player.Level.Value = 150
end

But it would probably execute the script before the player was on so you would want to try something more like this...

game.Players.PlayerAdded:connect(function()
for _, player in pairs(game.Players:GetPlayers()) do
	player.Level.Value = 150
end
end)

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