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

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

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)

4 comments

0

When you press play, or test a local server, does your player or do the players have an object called "leaderstats" in them?

0

nvm someone already answered

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

0

@Cvieyra2test I want it to be locating the player that is playing the game.

1 answer

✓ Accepted answer

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

1 comment

0

How can I give it to a localplayer?

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