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)

6 comments
Line 02, you're missing an assignment operator.
that is true, but scope is not needed, i know because my datastore system doesnt use scope either, it isnt causing the error
could you send us the error you are getting, we will have a better idea of the problem you are experiencing then
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.
...... 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.
Line 02 is the cause for the asker's problem..... what are you on about, especially with scope??