Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can someone help me with DataStore?

Asked by 10 years ago

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.

01local KEY_PREFIX = "userid_"
02local ds = game:GetService("DataStoreService"):GetDataStore("PlayerValuesDataStore")
03local profilesHolder = game.Workspace.Profiles
04function saveToDataStore(plr)
05    local key = KEY_PREFIX .. tostring(plr.userId)
06    local profile = profilesHolder[plr.Name]
07    local value = {}
08    for i,v in ipairs(profile:GetChildren()) do
09            value[v.Name] = v.Value
10        end
11    ds:SetAsync(key, value)
12end
13 
14function loadFromDataStore(plr)
15    local key = KEY_PREFIX .. tostring(plr.userId)
View all 35 lines...

And here's the script that creates the values.

01game.Players.PlayerAdded:connect(function(player)
02    Profile = Instance.new("Model")
03    Profile.Parent = script.Parent
04    Profile.Name = (player.Name)   
05    Cash = Instance.new("NumberValue")
06    Cash.Parent = script.Parent[player.Name]
07    Cash.Name = "Cash"
08    Cash.Value = 0
09    a = Cash:Clone()
10    a.Name = "WaitTime"
11    a.Parent = script.Parent[player.Name]
12    a.Value = 5
13    a = Cash:Clone()
14    a.Name = "Block1"
15    a.Parent = script.Parent[player.Name]
16    a = Cash:Clone()
17    a.Name = "Block2"
18    a.Parent = script.Parent[player.Name]
19end)

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.

1 answer

Log in to vote
1
Answered by 10 years ago

Something like this should work.

01--[[
02    DON'T TOUCH THESE FIRST VALUES!
03]]
04local KEY_PREFIX = "userid_" --This is a constant variable to prefix datastore keys
05local ds = game:GetService("DataStoreService"):GetDataStore("PlayerValuesDataStore") --This is how to get a datastore.  "PlayerValuesDataStore" is the name of the data store.
06 
07--[[
08    Please change this variable as noted below
09]]
10local profilesHolder = game.Workspace.Profiles --Change this to home of profiles
11 
12--[[
13    Here are your functions.
14]]
15function saveToDataStore(plr) --Define a function
View all 32 lines...

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.

0
Can you explain how this work please. I'm wanting to know the structure. DevWork 80 — 10y
0
I'll add comments to each line. NoahWillCode 370 — 10y
0
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. jaytheidiot 60 — 10y
0
Just commented it all. NoahWillCode 370 — 10y
View all comments (8 more)
0
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? jaytheidiot 60 — 10y
0
If you read through all the comments, you'll know which one value to change. And do you know how to call functions? NoahWillCode 370 — 10y
0
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. jaytheidiot 60 — 10y
0
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! NoahWillCode 370 — 10y
0
I'm going to post an answer as to demonstrate my problems. jaytheidiot 60 — 10y
0
Uhm...That's not an answer though. How about you just edit your question? NoahWillCode 370 — 10y
0
Too late for that. I'm off to work. jaytheidiot 60 — 10y
0
For some reason, I can't comment on your "answer"... NoahWillCode 370 — 10y
Ad

Answer this question