What do you want to achieve?
I’m creating a character creation system in-game but I have so many values I don’t want to have a different datastore code for all of them and decided to figure out how to save em all as in a table. (JSON)
What is the issue?
I can’t get it to save / load or both.
What solutions have you tried so far?
I’ve followed several devforum tutorials but I just can’t seem to get it to work.
Player Joined:
local characterdata = {}
local successCharacter, ErrorCharacter = pcall(function()
game:GetService("DataStoreService"):GetDataStore("CharacterDatastoreTest_1" .. player.UserId):GetAsync(player.UserId)
end)
if successCharacter then
characterdata = game:GetService("HttpService"):JSONDecode(characterdata)
if characterdata.FirstName ~= nil then
print("Found Name in table")
firstname.Value = characterdata.FirstName
end
else
warn(ErrorCharacter)
end
Player Removing:
local characterdata = {}
for i, v in pairs(player.CharacterStats.Character:GetChildren()) do
characterdata[v.Name] = v.Value
end
local successCharacter, ErrorCharacter = pcall(function()
game:GetService("DataStoreService"):GetDataStore("CharacterDatastoreTest_1" .. player.UserId):SetAsync(player.UserId, game:GetService("HttpService"):JSONEncode(characterdata))
end)
if successCharacter then
print("Table saved")
else
warn(ErrorCharacter)
end
Wait hold on, I did this and my datastore suddenly worked, is there something wrong or is this alright, I never work with tables or JSON I don’t really know
local dataservice = GetService('DataStoreService')--edited
local datastore = dataservice:GetDataStore('PlayerData')--edited
game.Players.PlayerAdded:Connect(function(player)--edited
local characterdata = {}
local successCharacter, ErrorCharacter = pcall(function()
datastore :GetAsync(player.UserId)--edited
end)
if successCharacter then
characterdata = game:GetService("HttpService"):JSONDecode(characterdata)
if characterdata.FirstName ~= nil then
print("Found Name in table")
firstname.Value = characterdata.FirstName
end
else
warn(ErrorCharacter)
end
end)--edited
game.Players.PlayerRemoving:Connect(function(player)--edited
local characterdata = {}
for i, v in pairs(player.CharacterStats.Character:GetChildren()) do
characterdata[v.Name] = v.Value
end
local successCharacter, ErrorCharacter = pcall(function()
datastore :SetAsync(player.UserId,characterdata)--edited
end)
if successCharacter then
print("Table saved")
else
warn(ErrorCharacter)
end
end)--edited
local function saveData(player)
--add your save script
end)
game.Players.PlayerRemoving:Connect(saveData)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
saveData(player)
end
end)
Alright, fixed up the code, thank you for mentioning, because I heard people lost data when shutdowns occured, now I now what this functions is for, glad I learned something new.
Hold on quick bump, I don’t know if you know this, but whenever a new player joins the game and his data is new it gives me an error “argument missing” something like that, it doesn’t happen after, is there a way to avoid that, if not it’s fine