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

Can someone help with my Data Store problem?

Asked by 9 years ago
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)

This is supposed to go through game.Workspace.Profiles and find all the values then save them and their values to a datastore. The problem is it is not working. It will print out values just fine when it saves, but I don't know if it's actually saving the data to it. Also, it will print "loaded" when I load, but not the loaded values because there is none to load or it is not fetching them properly. Can someone please help solve this issue?

Answer this question