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

Can someone help with my Data Store problem?

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
Debounce = true

function saveToDataStore(plr)
    if Debounce == true then
        Debounce = false    
    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
        print(v.Name .. " = " .. tostring(v.Value))
    end
    ds:SetAsync(key, value)
    wait(15)
    print("Saved")
    Debounce = true
    elseif Debounce == false then
        print("Not ready")
end
end

function loadFromDataStore(plr)
    if Debounce == true then
        Debounce = false    
    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(data) do
        profileItems[i].Value = v
        print(i .. " = " .. tostring(v))
    end
    wait(15)
    print("Loaded")
    Debounce = true
    elseif Debounce == false then
        print("Not ready")
    end
end

game.Players.PlayerAdded:connect(function(player)
    player.Chatted:connect(function(msg)
        msg = msg:lower()
        if msg == "/save" then
            saveToDataStore(player)
        elseif msg == "/load" then
            loadFromDataStore(player)

        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)

2 answers

Log in to vote
0
Answered by 10 years ago

I'm sorry - I was really tired when I tried to help you last night and I had one small error in my code. I do sincerely apologize. The below code will work.

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
        print(v.Name .. " = " .. tostring(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(data) do
        profileItems[i].Value = v
        print(i .. " = " .. tostring(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)

0
When I save, it prints Saved. When I load it prints Loaded. It doesn't break the money gathering system anymore, but it doesn't load/save the data either. Is it a problem I created with my Chatted script? I could possibly not be gathering player correctly. jaytheidiot 60 — 10y
0
I've updated my script so that it will print the names of each item in the profile and their values on save and load. Mind replacing the script again and telling me what that looks like? NoahWillCode 370 — 10y
0
The problem is in the Loading. It saves and prints all the values, but when I load it prints Loaded but nothing else jaytheidiot 60 — 10y
0
Are you waiting at least 15 seconds in between the "/save" and "/load" commands? ROBLOX's DataStore caches every ten seconds, so you need to. NoahWillCode 370 — 10y
View all comments (10 more)
0
I will make it wait before trying, and then try again jaytheidiot 60 — 10y
0
It's still not working. I added the wait. And updated script can be found in the question. It doesn't print or load a list of values after loading. When I save it lists all the values however. jaytheidiot 60 — 10y
0
Are there any errors (red text)? NoahWillCode 370 — 10y
0
None at all jaytheidiot 60 — 10y
0
Well, we do have 15:40:38.156 - httpGet https://api.roblox.com/gametransactions/getpendingtransactions/?PlaceId=10965599&PlayerId=54680 failed. Trying again. Error: Forbidden. Elapsed time: 0.193088 15:40:38.157 - httpGet failed. Trying again. Elapsed time: 0.193088 jaytheidiot 60 — 10y
0
No Red however jaytheidiot 60 — 10y
0
That httpGet has nothing to do with this. Okay...Can you give me a link to the game that this is at? NoahWillCode 370 — 10y
0
It's only printing to server console, not really sure how to get it to print to the local console. jaytheidiot 60 — 10y
0
I sent you a link on Twitter for a download of the place jaytheidiot 60 — 10y
Ad
Log in to vote
-1
Answered by 10 years ago

I dont know if this will work but use the ":SaveNumber()" method here is the link

http://wiki.roblox.com/index.php?title=SaveNumber_(Method)

here is to load the ":LoadNumber()" method

http://wiki.roblox.com/index.php?title=LoadNumber_(Method)

0
He doesn't want to use data persistence, he wants to use data stores. NoahWillCode 370 — 10y

Answer this question