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

Attempt to index nil leaderstats? [closed]

Asked by 4 years ago

Please provide more explanation in your question. If you explain exactly what you are trying to accomplish, it will be much easier to answer your question correctly.

hey there so I'm making a game and a sell area for points ok? so when i touch the brick my script is:

script.Parent.Touched:Connect(function() if game.Players.LocalPlayer.leaderstats.Potatoes.Value == 6 then game.Players.LocalPlayer.leaderstats.Cash.Value = game.Players.LocalPlayer.leaderstats.Cash.Value +10 end end)

and it get this error:Attempt to index nil with leaderstats

Closed as Not Constructive by EzraNehemiah_TF2

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

1 answer

Log in to vote
0
Answered by
raid6n 2196 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

First, make this a server script, it won't work if it's a local script. (I also did getplayerfromcharacter because you cant get a localplayer from a server script.)

Second, use waitforchild just in case the leaderstats hasn't loaded yet!

edited code:

script.Parent.Touched:Connect(
    function(hit)
        local Player = game.Players.GetPlayerFromCharacter(hit.Parent)
        if Player:WaitForChild("leaderstats"):WaitForChild("Potatoes").Value == 6 then
            Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + 10
        end
    end
)

Accept answer if it works.

0
local currencyName = "Cash" game.Players.PlayerAdded:Connect(function(player) local folder = Instance.new("Folder") folder.Name = ("leaderstats") folder.Parent = player local currency = Instance.new("IntValue") currency.Name = currencyName currency.Parent = folder local currency = Instance.new("IntValue") currency.Name = ("Potatoes") currency.Parent = folder end) Cats767_99 -68 — 4y
Ad