Hi, this is my datastore2 script and I'm trying to save data using a table. but for some reason I got the error:
"Attempt to index nil with 'Increment' line 126"
I had tried printing on line 85 and the result was:
{
["Deaths"] = 0;
["Kills"] = 0;
["Golds"] = 0;
["Gems"] = 0;
}
and I tried printing on line 86 with the result:
{
[1] = "Wooden Spear";
[2] = "Wooden Sword";
}
and line 87 result was:
0
The script:
local DataStore2 = require(1936396537)
DataStore2.Combine("DATA", "Status", "Weapons", "Inventory")
local function settbl()
local data = {
Status = {
["Kills"] = 0;
["Deaths"] = 0;
["Golds"] = 0;
["Gems"] = 0;
};
Weapons = {
["Melee"] = {"Wooden Spear","Wooden Sword"};
["Range"] = {"Wooden Bow"};
["Spell"] = {};
};
Inventory = {
["AncientRuins"] = 0;
["Sticks"] = 0;
["BlueGems"] = 0;
["RedGems"] = 0;
["GreenGems"] = 0;
["IronIngots"] = 0;
["GoldIngots"] = 0;
["MagicMushrooms"] = 0;
};
}
return data
end
function playeradd(plr)
local dat = DataStore2("DATA",plr):GetTable(settbl())
-- leaderboard
local stat = Instance.new("Folder")
stat.Name = "leaderstats"
stat.Parent = plr
local store = Instance.new("Folder")
store.Name = "Storage"
store.Parent = plr
local weap = Instance.new("Folder")
weap.Name = "Weapons"
weap.Parent = plr
local mele = Instance.new("Folder")
mele.Name = "Melee"
mele.Parent = weap
local spel = Instance.new("Folder")
spel.Name = "Spell"
spel.Parent = weap
local rang = Instance.new("Folder")
rang.Name = "Range"
rang.Parent = weap
local kills = Instance.new("IntValue")
kills.Name = "Kills"
kills.Parent = stat
local deaths = Instance.new("IntValue")
deaths.Name = "Deaths"
deaths.Parent = stat
local gold = Instance.new("IntValue")
gold.Name = "Golds"
gold.Parent = store
local gem = Instance.new("IntValue")
gem.Name = "Gems"
gem.Parent = store
-- Saving/Loading
local statdata = DataStore2("Status",plr)
local statweapon = DataStore2("Weapons",plr)
local function updatastat(new)
kills.Value = statdata:Get(new).Kills
deaths.Value = statdata:Get(new).Deaths
gold.Value = statdata:Get(new).Golds
gem.Value = statdata:Get(new).Gems
end
print(statdata:Get())
print(statweapon:Get())
print(statdata:Get().Kills)
-- Loading Stats
updatastat(dat.Status)
-- Loading Weapons
if statweapon:Get().Melee ~= nil then
local nummele = statweapon:Get().Melee
for i,v in pairs(nummele) do
local strig = Instance.new("StringValue")
strig.Name = v
strig.Parent = mele
end
end
if statweapon:Get().Range ~= nil then
local numrang = statweapon:Get().Range
for i,v in pairs(numrang) do
local strig = Instance.new("StringValue")
strig.Name = v
strig.Parent = rang
end
end
if statweapon:Get().Spell ~= nil then
local numspel = statweapon:Get().Spell
for i,v in pairs(numspel) do
local strig = Instance.new("StringValue")
strig.Name = v
strig.Parent = spel
end
end
-- Update Stats
statdata:OnUpdate(updatastat)
plr.PlayerGui.ScreenGui.LocalScript.Disabled = false
-- On death event
plr.CharacterAdded:Connect(function(char)
local humanoid = char:FindFirstChild("Humanoid")
humanoid.WalkSpeed = humanoid.WalkSpeed + 2
humanoid.Died:Connect(function()
statdata.Deaths:Increment(1)
end)
end)
end
for _, player in pairs(game.Players:GetPlayers()) do
playeradd(player)
end
game.Players.PlayerAdded:Connect(playeradd)
Help please, thank you!
