From what I see, this part of code..
game.Players.PlayerRemoving:connect(function(plr)
local key = 'Player-'..plr.UserId
for i = 1, 3 do
value.Value = dataStore:SetAsync(key,valueName[i])
end
end)
Most of your code is correct, but so far, this should be the only problem I've spot.
From your code, above
- You're overwriting the key 3 times.
- You're saving a "part" of the table, or what I can say, a ordinary string.
- You're setting a value into a value (WHAAT!?)
For sure, if you want this to be easily done. Perhaps this is my suggestion
WARNING: Code not checked
local dataStore = game:GetService('DataStoreService'):GetDataStore('Data0')
local HS = game:GetService("HttpService")
local valueName = {
'EXP',
'Level',
'Points'
}
local valuesValue = {
'IntValue',
'IntValue',
'IntValue'
}
local defaultValue = {
0,
0,
0
}
game.Players.PlayerAdded:connect(function(plr)
local stats = Instance.new('Folder' ,plr)
stats.Name = 'Stats'
local key = "Player-"..plr.UserId
local data = dataStore:GetAsync(key)
print(data)
if type(data) == "string" then
data = HS:JSONDecode(data)
else
data = {}
end
for k,v in pairs(valueName) do
if not data[v] then
data[v] = defaultValue[k]
end
end
for k,v in pairs(valueName) do
local value = Instance.new(valuesValue[k], stats)
value.Name = v
value.Value = data[v]
end
end)
game.Players.PlayerRemoving:connect(function(plr)
local key = 'Player-'..plr.UserId
local PlayerData = plr:findFirstChild("Stats")
if not PlayerData then print("Uhh... Saving Failed?") return end
local data = {}
for k,v in pairs(valueName) do
local dat = PlayerData:findFirstChild(v)
if not dat then return end
data[v] = dat.Value
end
print(HS:JSONEncode(data))
dataStore:SetAsync(key, HS:JSONEncode(data))
end)
Theoritically, should work.
2 comments
Please explain whats wrong. you didn't explain anything about your script.
You'll be overwriting the same key three times when you save. You should probably just save the table itself.