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

My DataSaving script sometimes just doesn't save! Why?!

Asked by 2 years ago

So i've been trying to make a material system but since im not entirely that good at scripting, i decided to save the materials that the player has collected throughout their play session to a leaderstats - like Folder (made specifically so it won't show up on the upper right corner, where the player list is) and i used DataStoreService (by following a tutorial of course) and after testing it TONS of times, it seemed to work. When i went to work on the game today though, some of the values were completely zero. What went wrong? Heres the script

local DataStore = game:GetService("DataStoreService"):GetDataStore("DataStore")

local ErrorMessage = "get noob no save lol"

game.Players.PlayerAdded:Connect(function(Player)
    local StatFolder = Instance.new("Folder")
    StatFolder.Name = "StatFolder"
    StatFolder.Parent = Player

    local Coal = Instance.new("IntValue")
    Coal.Name = "Coal"
    Coal.Parent = StatFolder

    local Iron = Instance.new("IntValue")
    Iron.Name = "Iron"
    Iron.Parent = StatFolder

    local Gold = Instance.new("IntValue")
    Gold.Name = "Gold"
    Gold.Parent = StatFolder

    local Norzium = Instance.new("IntValue")
    Norzium.Name = "Norzium"
    Norzium.Parent = StatFolder

    local OakWood = Instance.new("IntValue")
    OakWood.Name = "OakWood"
    OakWood.Parent = StatFolder

    local Copper = Instance.new("IntValue")
    Copper.Name = "Copper"
    Copper.Parent = StatFolder

    local CoalData
    local IronData
    local GoldData
    local NorzData
    local OakData
    local CopperData

    local Success, ErrorMessage = pcall(function()
        CoalData = DataStore:GetAsync(Player.UserId.."-CoalData")
        IronData = DataStore:GetAsync(Player.UserId.."-IronData")
        GoldData = DataStore:GetAsync(Player.UserId.."-GoldData")
        NorzData = DataStore:GetAsync(Player.UserId.."-NorzData")
        OakData = DataStore:GetAsync(Player.UserId.."-OakData")
        CopperData = DataStore:GetAsync(Player.UserId.."-CopperData")
    end)

    if not Success then
        print(ErrorMessage)
    end

    if CoalData ~= nil then
        Coal.Value = CoalData
    else
        Coal.Value = 0
    end

    if CopperData ~= nil then
        Copper.Value = CopperData
    else
        Copper.Value = 0
    end

    if OakData ~= nil then
        OakWood.Value = OakData
    else
        OakWood.Value = 0
    end

    if NorzData ~= nil then
        Norzium.Value = NorzData
    else
        Norzium.Value = 0
    end

    if GoldData ~= nil then
        Gold.Value = GoldData
    else
        Gold.Value = 0
    end

    if IronData ~= nil then
        Iron.Value = IronData
    else
        Iron.Value = 0
    end
end)

game.Players.PlayerRemoving:Connect(function(Player)
    pcall(function()
        DataStore:SetAsync(Player.UserId.."-CoalData", Player.StatFolder.Coal.Value)
        DataStore:SetAsync(Player.UserId.."-IronData", Player.StatFolder.Iron.Value)
        DataStore:SetAsync(Player.UserId.."-GoldData", Player.StatFolder.Gold.Value)
        DataStore:SetAsync(Player.UserId.."-NorzData", Player.StatFolder.Norzium.Value)
        DataStore:SetAsync(Player.UserId.."-OakData", Player.StatFolder.OakWood.Value)
        DataStore:SetAsync(Player.UserId.."-CopperData", Player.StatFolder.Copper.Value)
    end)
end)

I hope someone answers this. Anyway, cheers!

Answer this question