Help on Save/Load script involving Async data

Hello! I am scripting a game of my own in which requires a lot of scripting and building. As I have been scripting for years, overtime I learned more and more. However, there are obviously road blocks in how to do some parts of scripting. I’ve been attempting a save/load script. I’ve looked at other people’s examples as I tend to teach myself most of the things I know today. There’s Async in which I looked into but got confused about it. I setup a script, for the basics I believe but I’m not sure where I continue.

Enough about the complex issue, here’s what I’m looking for. I’m looking for steps on what to do next. I’ve made the script so far in which is in the pastebin link below. I’m looking to complete this script with all the values in which a part of it. There’s going to possibly be even more to have it save and load.

Due to so much, I might end up making it a manual save and an automatic load since it seems like a lot of data to be saved when the player hits exit on the roblox client.

Script: https://pastebin.com/YVXGcP65

I apologize if it’s weird. The repeat with the debounces is how I always double-check code so that it knows that it exists before continuing on into the next section of code. It’s to prevent errors saying, “Oh I’m sorry, this doesn’t exist.”

It seems you are saving the data in a folder located in the PlayerGui. This is really odd, if you want to store player data in a folder, store all the players’ folders maybe in a folder in the ReplicatedStorage. Also I don’t see you using SetAsync() and GetAsync() anywhere which doesn’t save or load your data.

I had attempted using it in another script. I created multiple versions but all failed. As I did say I was looking at other examples.

By reading your message, you’re saying I should set up the save/load script so that all the data goes into replicatedStorage? If it’s a new player, it’ll create a new folder with the players ID and then put default data there along with changing it overtime as they progress through the game?

Am I getting that correct?

Personally, I’d create a folder for someone when they join in a game through a global script in ServerScriptService.

Something along the lines of this:

game.Players.PlayerAdded:connect(function(player)
     wait(1) -- Give the player a moment to load, probably not crucial but I personally do this.
     local folder = Instance.new("Folder", player)
     folder.Name = player.Name -- This can make it handy to find via local scripts if and when needed by simply going "game.Players.LocalPlayer:FindFirstChild(game.Players.LocalPlayer.Name)"
     local exampleStat = Instance.new("IntValue", folder)
     -- More values would go below this line if needed, this should all be done before the datastore has any say in what happens next
end

That’s a good way of constructing a DataStore system in terms of where things are placed.

One major mistake I made when programming my first datastore was doing it in a local script, last time I did that it didn’t give any errors, so it’s difficult to realise when you’re new to this sort of thing. Make sure it’s within a script in ServerScriptService.

When it comes to UpdateAsync() on a player leaving, you can do it like this,

local exampleDatastore = game:GetService("DataStoreService"):GetDataStore("ExampleDataStore")

game.Players.PlayerRemoving:connect(function(player)
local dataKey = player.UserId -- Don't ever use their names because if they change it, their data won't be accessable by them anymore
function updateAsync()

exampleDatastore:UpdateAsync(dataKey, function(oldValue)
local newValue = oldValue or 0
newValue = newValue
return newValue
end)
end

local success, message = pcall(updateAsync)

if success then
print("You can print something like 'Success' here, useful for debugging")
else
print("Something went wrong, another print here saying something like 'Something went wrong: '"..message.."")

end)

In terms of saving tables, I’m not as great with that, haven’t got round to doing that in the current thing I’m working on, but it will work for individual values, I know it’s not the most convenient, but it’s what I know, hope I was somewhat helpful! Good luck with your DataStore

1 Like

That was what I was looking for. Whenever I usually do scripting, I do it in both Script and Local Script as to see which one works and learn from that and then on.

Thanks for the help!

1 Like