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

Attempt to index nil leaderstats?

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

1 answer

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.

1 comment

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)

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