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

datastore problem (data can't save)?

Asked by
Rare_tendo 3000 Moderation Voter Community Moderator
6 years ago

I need help with datastores, appearantly this module script doesn't work

local module = {}

module.BaseStats = {
    Credits = 0;
    Rank = 1;
    EXP = 0;
    Inventory = {
        AchievementPins = {}
    }
}

local dss = game:GetService("DataStoreService"):GetDataStore('PlayerData')

function save(player)
    local tries = 0
    local saved = false
    repeat wait()
        local inventory={
            AchievementPins={};
        }
        for _,v in pairs(player.Stats.Inventory.AchievementPins:GetChildren()) do
            inventory.AchievementPins[#inventory.AchievementPins+1] = v.Name
        end
        local success,message = pcall(function()
            dss:SetAsync('user_'..player.UserId,{
                Credits = player.Stats.Credits.Value;
                Rank = player.Stats.Rank.Value;
                EXP = player.Stats.EXP.Value;
                Inventory = inventory;
            })
        end)
        print("Attempting to save "..player.Name.."'s data("..tries..")")
        if success then
            saved = true
            print("Successfully saved "..player.Name.."'s data")
        else
            tries = tries + 1
            print("Attempting to save "..player.Name.."'s data("..tries..")")
        end
        wait()
    until (tries == 5) or saved
end

function module.ClientJoined(player)
    local stats = Instance.new('Folder',player)
    local credits = Instance.new('IntValue',stats)
    local rank = Instance.new('IntValue',stats)
    local exp = Instance.new('IntValue',stats)
    local inventory = Instance.new('Folder',stats)
    local achievementpins = Instance.new('Folder',inventory)

    stats.Name = 'Stats'
    credits.Name = 'Credits'
    rank.Name = 'Rank'
    exp.Name = 'EXP'
    inventory.Name = 'Inventory'
    achievementpins.Name = 'AchievementPins'

    local data = dss:GetAsync('user_'..player.UserId) or module.BaseStats

    credits.Value = data.Credits
    rank.Value = data.Rank
    exp.Value = data.EXP

    for _,v in pairs(data.Inventory) do
        for _,value in pairs(v) do
            Instance.new('StringValue',stats[v.Name]).Name = value
        end
    end
end

function module.ClientLeft(player)
    save(player)
end

game.Players.ChildAdded:Connect(function(player) module.ClientJoined(player) end)
game.Players.ChildRemoved:Connect(function(player) module.ClientLeft(player) end)

return module

Answer this question