So, I made this out of what CodeTheorem gave me. When I say /save, it prints Saved. When I say /load, it prints loaded. But, when I load after saving, my cash gets set to 0 and doesn't continue to increase. After reading through the code, to me it seems like it's saving the names of the Values and not the actual values of them. When I load from the Data Store, I want it to set each Value to the value they were at when I saved.
local KEY_PREFIX = "userid_" local ds = game:GetService("DataStoreService"):GetDataStore("PlayerValuesDataStore") local profilesHolder = game.Workspace.Profiles function saveToDataStore(plr) local key = KEY_PREFIX .. tostring(plr.userId) local profile = profilesHolder[plr.Name] local value = {} for i,v in ipairs(profile:GetChildren()) do value[v.Name] = v.Value end ds:SetAsync(key, value) end function loadFromDataStore(plr) local key = KEY_PREFIX .. tostring(plr.userId) local data = ds:GetAsync(key) local profile = profilesHolder[plr.Name] local profileItems = profile:GetChildren() for i,v in ipairs(profile:GetChildren()) do profileItems[i].Value = v end end game.Players.PlayerAdded:connect(function(player) player.Chatted:connect(function(msg) msg = msg:lower() if msg == "/save" then saveToDataStore(player) print("Saved") elseif msg == "/load" then loadFromDataStore(player) print("Loaded") end end) end)
And here's the script that creates the values.
game.Players.PlayerAdded:connect(function(player) Profile = Instance.new("Model") Profile.Parent = script.Parent Profile.Name = (player.Name) Cash = Instance.new("NumberValue") Cash.Parent = script.Parent[player.Name] Cash.Name = "Cash" Cash.Value = 0 a = Cash:Clone() a.Name = "WaitTime" a.Parent = script.Parent[player.Name] a.Value = 5 a = Cash:Clone() a.Name = "Block1" a.Parent = script.Parent[player.Name] a = Cash:Clone() a.Name = "Block2" a.Parent = script.Parent[player.Name] end)
At first I had no idea what the script was supposed to do, but then it occurred to me at work that it wasn't supposed to be mixed up in my Profile spawning script and was supposed to use its own script to save the data in it.
Something like this should work.
--[[ DON'T TOUCH THESE FIRST VALUES! ]] local KEY_PREFIX = "userid_" --This is a constant variable to prefix datastore keys local ds = game:GetService("DataStoreService"):GetDataStore("PlayerValuesDataStore") --This is how to get a datastore. "PlayerValuesDataStore" is the name of the data store. --[[ Please change this variable as noted below ]] local profilesHolder = game.Workspace.Profiles --Change this to home of profiles --[[ Here are your functions. ]] function saveToDataStore(plr) --Define a function local key = KEY_PREFIX .. tostring(plr.userId) --Get the key to store the user's data. Notice I'm using the player's userId - THIS IS VERY IMPORTANT! Their userId doesn't change, but their username can! local profile = profilesHolder[plr.Name] --Get their profile from wherever you're storing those local value = {} --This is a table where we'll store all of their values for i,v in ipairs(profile:GetChildren()) do --Start a for each loop through the profile value[v.Name] = v.Value --Add each profile value to the 'value' table end --End the for each loop ds:SetAsync(key, value) --Save all the profile data to the data store end -- End function function loadFromDataStore(plr) --Call this after creating the profile (This defines a function) local key = KEY_PREFIX .. tostring(plr.userId) --Get the key to the user's data again local data = ds:GetAsync(key) --Load their data local profile = profilesHolder[plr.Name] --Get their profile local profileItems = profile:GetChildren() --Get all the items in their profile for i,v in ipairs(profile:GetChildren()) do --Loop through their data in the datastore profileItems[i].Value = v --Change the value of the appropriate profile object to the data in the datastore end -- End the foreach loop end --End the function
How this works
For the saveToDataStore
function
, this script
will loop
through all of the items in a player's Profile
, add the values
to a table
using their Name
as the key
, and then save that table
to the datastore
.
As for the loadFromDataStore
function
, it will loop
through all of the keys
in the table
from the datastore
, and appropriately change the corresponding value
of the Object
in the player's Profile
.
I recommend reading this for more help.