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

Trying to change the leaderstats value but it return (a nil value)?

Asked by 5 years ago
Player = game.Players:GetPlayers()

Player.leaderstats[Currency].Value = Player.leaderstats[Currency].Value + 30

Yet That the code can anyone help me this make me so confusing for this whole day god damn it.

Is the player variable wrong ?

Btw the error message is

 23:32:53.333 - Workspace.Grass.Script:80: attempt to index field 'leaderstats' (a nil value)
0
When you press play, or test a local server, does your player or do the players have an object called "leaderstats" in them? Cvieyra2test 176 — 5y
0
nvm someone already answered Cvieyra2test 176 — 5y
0
@Cvieyra2test yes it have but until now I still can't figure out even thought there is answer but I still can't locate the player correctly AustinTan0615 2 — 5y
0
@Cvieyra2test I want it to be locating the player that is playing the game. AustinTan0615 2 — 5y

1 answer

Log in to vote
1
Answered by
atawok 33
5 years ago
Edited 5 years ago

I'll assume you have the leaderstats already set up, but if you don't, I have attached a link at the bottom of this answer that will help you do that.

Player = game.Players:GetPlayers()

Player.leaderstats[Currency].Value = Player.leaderstats[Currency].Value + 30

The problem here is that the variable "Player" is referring to a table of players, which is what :GetPlayers() returns. It then tries to search that table for a value "leaderstats," but does not find it. In order to change each player's stats, you'll have to loop through the table of players. Here is a solution that will add to each player's stat.

local Players = game.Players:GetPlayers()

for i = 1, #Players do -- this will loop through each value(players) in Players
    Players[i].leaderstats[Currency].Value = Player[i].leaderstats[Currency].Value + 30
end

If you want to give stats to a specific player:

local Player = game.Players.PlayerNameHere

Player.leaderstats[Currency].Value = Player.leaderstats[Currency].Value + 30

Here are some resources on loops and creating leaderstats: https://developer.roblox.com/articles/Roblox-Coding-Basics-Loops https://developer.roblox.com/articles/Leaderboards

0
How can I give it to a localplayer? AustinTan0615 2 — 5y
Ad

Answer this question