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

Auto-Save Data Store Coming Out With Error: attempt to index nil with 'leaderstats'?

Asked by 3 years ago

I am trying to expand my scripting knowledge with what I see as the most important part of a game, data stores. I have watched video after video, article after article and still come out with errors, help would be greatly appreciated.

Code:

local DataStoreService = game:GetService('DataStoreService')
local DataSave = DataStoreService:GetDataStore('SaveData')

game.Players.PlayerAdded:Connect(function(player)
    local Leaderstats = Instance.new('Folder')
    Leaderstats.Name = 'leaderstats'
    Leaderstats.Parent = player

    local Cash = Instance.new('IntValue')
    Cash.Name = 'Cash'
    Cash.Parent = Leaderstats

    local data
    local playerID = player.UserId

    local success, errmessage = pcall(function()
        data = DataSave:GetAsync(playerID..'-Cash')
    end)

    if success then
        Cash.Value = data
    else
        warn(errmessage)
    end
end)

while wait(30) do
    for number, player in pairs(game.Players:GetChildren()) do
        local playerID = player.UserId
        local success, errormessage = pcall(function(player)
            DataSave:SetAsync(playerID..'-Cash', player.leaderstats.Cash.Value)
        end)

        if success then
            print("Succes!")
        else
            warn(errormessage)
        end
    end
end 
  • wolftamer894

1 answer

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago
local success, errormessage = pcall(function(player) -- try removing player from this and making it local success, errormessage = pcall(function() 
    DataSave:SetAsync(playerID..'-Cash', player.leaderstats.Cash.Value)
end)

also might want to use game.Players:GetPlayers()

0
Thank You So Much! wolftamer894 50 — 3y
Ad

Answer this question