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.

12 comments
Can you explain how this work please. I'm wanting to know the structure.
I'll add comments to each line.
Ok, so, I get some of it. Not all of it however... Keep in mind I have never used Data Store before. I don't know how I would use this to write my variables.
Just commented it all.
So, I just put this all in the script I already have after it? Is this going to collect every Value I tell the script I had prior and save values to them? Or does the script you just gave me require me to add the values somewhere in that script?
If you read through all the comments, you'll know which one value to change. And do you know how to call functions?
I didn't notice the bit of info under the script... So, I use the load one on my script, loading it after I create all the Values. Then I can run the save on any script to change it? Sorry if I'm being difficult.
No, sadly, you can't use a function across many scripts in ROBLOX without a module script or bindable / remote functions. But you can call the saveToDataStore function anywhere else in your script very easily! And that's okay!! I'd rather all new scripters ask questions - it's the only way to learn!
I'm going to post an answer as to demonstrate my problems.
Uhm...That's not an answer though. How about you just edit your question?
Too late for that. I'm off to work.
For some reason, I can't comment on your "answer"...