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

Data store not saving giving error on line 20?

Asked by
hokyboy 270 Moderation Voter
3 years ago

The error on line 20 = Attampt to index nil with setasync

I always hated data stores and i find them really hard so can u guys help me out

local DataStore = game:GetService("DataStoreService")
local GameData DataStore:GetDataStore("Beta1")

local baseStats = {

    Health = 100;
    Stamina = 50;
    Defence = 5;
    Damage = 10;
    Level = 1;
    Exp = 0;
}

function SaveData(Player,PlrStats)
    local dataToSave = {}
    warn("Saving ",Player," Data")
    for i,data in pairs(PlrStats:GetChildren()) do
        dataToSave[i] = data.Value
    end
    GameData:SetAsync(Player.UserId,dataToSave)
    warn("Data Saved")
end

game.Players.PlayerAdded:Connect(function(Player)
    local PlrStats = Instance.new("Folder",Player)
    PlrStats.Name = "DATA"

    local Health = Instance.new("NumberValue",PlrStats)
    Health.Name = "Health"
    Health.Value = baseStats.Health

    local Stamina = Instance.new("NumberValue",PlrStats)
    Stamina.Name = "Stamina"
    Stamina.Value = baseStats.Stamina

    local Defence = Instance.new("NumberValue",PlrStats)
    Defence.Name = "Defence"
    Defence.Value = baseStats.Defence

    local Damage = Instance.new("NumberValue",PlrStats)
    Damage.Name = "Damage"
    Damage.Value = baseStats.Damage

    local Level = Instance.new("NumberValue",PlrStats)
    Level.Name = "Level"
    Level.Value = baseStats.Level

    local EXP = Instance.new("NumberValue",PlrStats)
    EXP.Name = "EXP"
    EXP.Value = baseStats.EXP

    local plrSaves
    pcall(function()
        PlrStats = GameData:GetAsync(Player.UserId)
    end)
    if plrSaves then
        -- Player Has Data
        warn("Player Has Data")
        for i, data in pairs(PlrStats:GetChildren()) do
            data.Value = plrSaves[i]
            warn(data.name,":",data.value)
        end

    else
        warn("Player Has No Data")
        for i,data in pairs(PlrStats:GetChildren()) do
            data.Value = baseStats[i]
            print(data.name,":",data.Value)
        end
    end
end)

game.Players.PlayerRemoving:Connect(function(Player)
    local PlrStats = Player:WaitForChild("DATA")
    SaveData(Player,PlrStats)
end)
1
Line 02, you're missing an assignment operator. Ziffixture 6913 — 3y
0
that is true, but scope is not needed, i know because my datastore system doesnt use scope either, it isnt causing the error Lord_WitherAlt 206 — 3y
0
could you send us the error you are getting, we will have a better idea of the problem you are experiencing then Lord_WitherAlt 206 — 3y
0
You seem to be trying to SetAsync PlrStats before it gets created. Backup the current script and then edit it to save the value after it gets created. I think it can't see the value that's why it says "nil"  Edit: basically set everything between line 14-22 to after line 51. DaggerSaber 14 — 3y
View all comments (2 more)
0
...... Attempt to index nil with %s means that the Object of the method or attribute referenced does not exist; the assignment operator gives meaning to the GameData variable, there is no allocation occurring (which should be raising a syntax error), therefore GameData is nil. Ziffixture 6913 — 3y
0
Line 02 is the cause for the asker's problem..... what are you on about, especially with scope?? Ziffixture 6913 — 3y

Answer this question