Ad
Log in to vote
0

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

Asked by 1 year ago

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)

0
I think take away the word "name" before "keaderstats" GottaHaveAFunTime 154 — 1y
0
do game.Players:FindFirstChild(name) Viking359 161 — 1y

2 answers

Log in to vote
0
Answered by 1 year ago

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]

Ad
Log in to vote
-1
Answered by 1 year ago

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.

0
I do have a leaderboard. hextrexs 7 — 1y
0
Your error indicates that 'leaderstats' doesn't exist in the player. If it does, your client is unable to find it. asdfasdf -2 — 1y
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". hextrexs 7 — 1y
0
Ok, Do you have any ideas on how i could fix that? hextrexs 7 — 1y
View all comments (16 more)
0
Mind if I come take a look at it? asdfasdf -2 — 1y
0
Oh, Like team create? hextrexs 7 — 1y
0
If so, Add my account, Name: "bestrindberg" (This account is not my main) hextrexs 7 — 1y
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. asdfasdf -2 — 1y
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? hextrexs 7 — 1y
0
I added you, if you don't mind I can come and explain it to you in Studio. asdfasdf -2 — 1y
0
No, I don't mind. hextrexs 7 — 1y
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. hextrexs 7 — 1y
0
Hello? hextrexs 7 — 1y
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. hextrexs 7 — 1y
0
Yeah, that's fine. I've added you already, it's literally just asdfasdf. asdfasdf -2 — 1y
0
Ok, Ill check my friend requests hextrexs 7 — 1y
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 hextrexs 7 — 1y
0
Nvm, I accepted hextrexs 7 — 1y
0
this is not even the problem creeperhunter76 546 — 1y
0
Glad I could get this solved for you! :) asdfasdf -2 — 1y

Answer this question