So, I made a table saving script that on load, makes stringValues (to show the data) in a folder that the script also makes. this is for my morph rp game. the stringValues aren’t being made for some reason.
local Players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
local httpService = game:GetService("HttpService")
function getCharacters(plr)
local success, result = pcall(function()
if not DSS:GetDataStore("CharacterSaves"):GetAsync(tostring(plr.UserId)) then
DSS:GetDataStore("CharacterSaves"):SetAsync(tostring(plr.UserId),httpService:JSONEncode({}))
end
return httpService:JSONDecode(DSS:GetDataStore("CharacterSaves"):GetAsync(tostring(plr.UserId)))
end)
if success then
return result
else
return {}
end
end
function loadCharacters(plr)
local charTable = getCharacters(plr)
local charFolder = Instance.new("Folder",game.ReplicatedStorage)
charFolder.Name = tostring(plr.UserId.. "_chars")
for i,v in pairs(charTable) do
local char = Instance.new("StringValue",charFolder)
char.Name = v
char.Value = v
end
end
function saveCharacters(plr)
local chars = game:GetService("ReplicatedStorage"):WaitForChild(tostring(plr.UserId).. "_chars"):GetChildren()
print("work")
chars = {}
for i,v in pairs(chars) do
if v:IsA("StringValue") then
print("inserted")
table.insert(chars,v.Value)
end
end
local success, result = pcall(function()
DSS:GetDataStore("CharacterSaves"):SetAsync(tostring(plr.UserId),httpService:JSONEncode(chars))
end)
if success then
print("Saved characters")
else
warn(result)
end
end
Players.PlayerAdded:Connect(function(plr)
loadCharacters(plr)
end)
Players.PlayerRemoving:Connect(saveCharacters)
2 things.
Please rename your post so it describes the basic problem.
In your post describe in detail what you expect to happen, and what is happening, just like the template for this forum asked you to do.
We can’t mind read. Please be really specific. It’s the best way to make informed decisions.
It’s like taking your car to a mechanic and saying ‘why isn’t it working?’. Do you mean the engine, or the windshield wipers, or the seat controls, or the chrome flaking off the hubcaps? The mechanic will tell you about the stuff they see and charge you thousands of dollars to fix it, but they might not fix the problem you originally were complaining about.
Why are you JSON encoding and decoding your data when setting and reading from the data store? Unless I’m mistaken, you shouldn’t need to since the data store service does this internally for you.
You’re grabbing the characters and placing them into chars. Then right after you’re setting chars to an empty table? That for loop in saveCharacters will never run since chars is empty.
There is a chance that the player will load before the script is read which is strange because the script should be read when the game begins but I suppose it’s not mutually exclusive. If it isn’t working and it’s solo play, it could be that it just isn’t triggering PlayerAdded.
Try adding this above the PlayerAdded trigger.
for _,plr in pairs(game.Players:GetPlayers()) do
loadCharacters(plr)
end
I think I tried everything that you guys recommended, but it’s still not working. here’s the edited script:
local Players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
local httpService = game:GetService("HttpService")
function getCharacters(plr)
local success, result = pcall(function()
if not DSS:GetDataStore("CharacterSaves"):GetAsync(tostring(plr.UserId)) then
DSS:GetDataStore("CharacterSaves"):SetAsync(tostring(plr.UserId),{})
end
return httpService:JSONDecode(DSS:GetDataStore("CharacterSaves"):GetAsync(tostring(plr.UserId)))
end)
if success then
print(result)
return result
else
return {}
end
end
function loadCharacters(plr)
local charTable = getCharacters(plr)
local charFolder = Instance.new("Folder",game.ReplicatedStorage)
charFolder.Name = tostring(plr.UserId)
for i,v in pairs(charTable) do
local char = Instance.new("StringValue",charFolder)
char.Name = v
char.Value = v
end
end
function saveCharacters(plr)
local charFold = game:GetService("ReplicatedStorage"):FindFirstChild(tostring(plr.UserId))
print(charFold)
if charFold then
local chars = charFold:GetChildren()
print(chars)
local newChars = {}
for i,v in pairs(chars) do
if v:IsA("StringValue") then
print("inserted")
table.insert(newChars,v.Value)
end
end
print(newChars)
local success, result = pcall(function()
DSS:GetDataStore("CharacterSaves"):SetAsync(tostring(plr.UserId),newChars)
end)
if success then
print("Saved characters")
else
warn(result)
end
else
warn("Error while finding folder")
end
end
for _,plr in pairs(game.Players:GetPlayers()) do
loadCharacters(plr)
end
Players.PlayerAdded:Connect(loadCharacters)
Players.PlayerRemoving:Connect(saveCharacters)
Can you show what the character folder layout looks like when you save? Based on the output here, it looks as though there are no children inside the character folder.
Also, how are you adding the string value inside the character folder? If you’re doing it from the client-side, the server won’t see that and that could explain why the server isn’t seeing any children in that folder.