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

I get error: "attempt to index field 'leaderstats' (a nil value)" How do i fix it?

I'm trying to make a script in wich gives a player a certain ammount of money based on what you typed in, But i get an error in wich i do not understand and i would like help.

Code:

function GiveMoney()
	local name = script.Parent:WaitForChild("Name")
	local money = script.Parent:WaitForChild("Money")
	game.Players.name.leaderstats.Money.Value = game.Players.name.leaderstats.Money.Value + money
end
script.Parent.Confirm.MouseButton1Click:connect(GiveMoney)

Error:

Players.bestrindberg.PlayerGui.Admin_GUI.GUI.Script:4: attempt to index field 'leaderstats' (a nil value)

2 comments

0

I think take away the word "name" before "keaderstats"

0

do game.Players:FindFirstChild(name)

2 answers

Easy fix. when you index like thing.name, it's the same as thing["name"], you don't want a string however, you want a variable. Instead of game.Players.name, do game.Players[name]

It seems to me that you don't currently have a leaderboard. Try using this.

First, let's create a Script in the Workspace and name it 'Stats'. Once we've done that, we can begin.

We'll begin with defining our leaderboard values. We'll use a nested array..

local boardStats = {
	{ "IntValue", "Money" }
}

Now that we have some values to play with, we'll need our function to usher our leaderboard into existence.

function LeaderBoard(player)
	local leaderstats = Instance.new("IntValue", player)
	leaderstats.Name = "leaderstats"
end

This will make the leaderboard visible in the PlayerList. Now we need to add our leaderboard values. To do this, we will use a for loop.

function LeaderBoard(player)
	local leaderstats = Instance.new("IntValue", player)
	leaderstats.Name = "leaderstats"

	for n in pairs(boardStats) do
		local stat = Instance.new(boardStats[n][1], leaderstats)
		stat.Name = boardStats[n][2]
	end
end

With this for loop, we're traversing the boardStats array, one nested array at a time. We come through the first time, and we'll find that boardStats[1] is equal to { "IntValue", "Money" } and we need to separate these. So we create an instance of boardStats[n][1] and place it into leaderstats. Once we've done that, we name the instance boardStats[n][2].

Last but not least, we need to connect the PlayerAdded function to get this to add values to every new player.

game:GetService("Players").PlayerAdded:connect(LeaderBoard)

Once that's done, altogether we have our leaderboard!

local boardStats = {
	{ "IntValue", "Money" }
}

function LeaderBoard(player)
	local leaderstats = Instance.new("IntValue", player)
	leaderstats.Name = "leaderstats"
	
	for n in pairs(boardStats) do
		local stat = Instance.new(boardStats[n][1], leaderstats)
		stat.Name = boardStats[n][2]
	end
end

game:GetService("Players").PlayerAdded:connect(LeaderBoard)

Your button should then be able to function.

20 comments

0

I do have a leaderboard.

0

Your error indicates that 'leaderstats' doesn't exist in the player. If it does, your client is unable to find it.

0

I have a script in ServerScriptService that adds a folder named "leaderstats" to everyone who joins, Inside the leaderstats folder i have a IntValue named "Money".

0

Ok, Do you have any ideas on how i could fix that?

0

Mind if I come take a look at it?

0

Oh, Like team create?

0

If so, Add my account, Name: "bestrindberg" (This account is not my main)

0

Yeah. I see your problem. ServerScriptService isn't replicated on the client. It's purely server-side. If you're going to have your leaderboard values in ServerScriptService, make a script to duplicate your values into the player.

0

Oh, Well the leaderstats folder is inside the player, It's just that the script to create that folder is in ServerScriptService. Should i move that script to like ReplicatedStorage or Workspace?

0

I added you, if you don't mind I can come and explain it to you in Studio.

0

No, I don't mind.

0

Did you send it it my "bestrindberg" account? hextrexs is my alt, Im only using it on here because for some reason my name was already taken.

0

Hello?

0

Please add the account "bestrindberg" and i will setup a team create world, I would rather not have you go into the actual game, So i will create a new one with all the scripts and things that we need.

0

Yeah, that's fine. I've added you already, it's literally just asdfasdf.

0

Ok, Ill check my friend requests

0

I can't find your request, You did send it to the account named: "bestrindberg" right? Dont send it to hex, This is my alt

0

Nvm, I accepted

0

this is not even the problem

0

Glad I could get this solved for you! :)

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