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.

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.

1 answer

Log in to vote
1
Answered by 10 years ago

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.

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