Hey everyone, thanks for checking this topic.
Recently, i’ve made the jump to Datastore2 after the previous datastore system caused issues, and i wanted our player’s data to be secure.
Right now we’re saving a stat system that counts playtime, kills, deaths, and abilities used.
It saves these stats for EACH character. Currently that’s 5, with a 6th on the way.
This is my current script, it only has 1 character for the sake of testing:
local datastore = require(game.ServerStorage.DataModule)
local key = "FakeKey"
local function SetupUserData()
local data = {
Statistics = {
Killer = {
Playtime = 0,
Kills = 0,
Deaths = 0,
AbilitiesUsed = 0,
}
},
}
return data
end
game.Players.PlayerAdded:Connect(function(plr)
local data = datastore(key,plr):Get(SetupUserData())
spawn(function()
while true do
print(data.Statistics.Killer.Playtime)
data.Statistics.Killer.Playtime += 1
wait(.1)
end
end)
end)
It works perfectly, but there’s one problem; i can’t add anything to the table or array without the script breaking.
I tried adding a new item to the table called Test
:
local function SetupUserData()
local data = {
Statistics = {
Killer = {
Playtime = 0,
Kills = 0,
Deaths = 0,
AbilitiesUsed = 0,
Test = 0,
}
},
}
return data
end
And i’ve changed the PlayerAdded
event to this:
game.Players.PlayerAdded:Connect(function(plr)
local data = datastore(key,plr):GetTable(SetupUserData())
spawn(function()
while true do
print(data.Statistics.Killer.Test)
data.Statistics.Killer.Test+= 1
wait(.1)
end
end)
end)
I thought this would work, but obviously it did not. And i have 0 clue on what to change to fix it.
This is the error that it gives:
attempt to perform arithmetic (add) on nil and number
Any help is appreciated, i’ll answer any questions that are thrown my way.
Oh, and this is my first time working with Datastore2, so any tips on how to improve the script are appreciated!
Thanks
-Spectro