I am making a game and I want to save/load stats using one single string. I use Data Store and String Pattern to save it.
local DS_Stats = game:GetService("DataStoreService"):GetDataStore("Stats")
local patern = ";(%a+):(%w+)" --this means that is search for ";" any letters ":" any letters or numbers"
function addStats(type,path,name,value)
local stat = Instance.new(type,path)
stat.Name = name
stat.Value = value
end
game.Players.PlayerAdded:connect(function (player)
local DS_key = "user_" .. player.userId
local Stats = Instance.new("Configuration",player)
Stats.Name = "Stats"
local ls = Instance.new("Configuration",player)
ls.Name = "leaderboard"
local Stat_String = DS_Stats:GetAsync(DS_key)
for key, val in Stat_String:gmatch(pattern) do --Problem is comming from this line It says Stat_String is a nil value, it was "for key, val in DS_Stats:GetAsync(DS_key):gmatch(pattern) do" before, but I tried making it a variable. Didn't worked.
if key == "LT" then
addStats("StringValue",Stats,"LT",val or "SpawnTown")
addStats("StringValue",ls,"LT",val or "SpawnTown")
elseif key == "OCU" then
addStats("IntValue",Stats,"OCU",val or 0)
addStats("IntValue",ls,"OCU",val or 0)
end
end
end)
game.Players.PlayerRemoving:connect(function(player)
local key = "user_" .. player.userId
local stats = player.Stats
DS_Stats:UpdateAsync(key, function(oldValue)
local newValue = oldValue or ";LT:SpawnTown;OCU:0"
newValue = ";LT:"..stats.LT..";OCU"..stats.OCU
return newValue
end)
end)
This is what it show on the DevConsole InGame ServerScriptService.OnEnter:20: attempt to index local "Stats_String" (a nil value) Stack Begin Script "ServerScriptService.OnEnter", Line 20 Stack End Content failed because Not Found
I am just Asking why it doesn't work and how to Fix It, Because i don't understand why the script act like the DataStore doesn't exist.

1 comment
And I know the tag is Data Persistance, but Data Store is kind of the new Data Persistance...