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

my datastore not save data ?

Asked by 3 years ago

script

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

game.Players.PlayerAdded:Connect(function(plr)

    local key = plr.UserId.."Key2"

    local data
    local success, errormessage = pcall(function()
        data = DataStore:GetAsync(key)
    end)

    local folder = Instance.new("Folder", plr)
    folder.Name = "data"

    local IronOr = Instance.new("NumberValue", folder)
    IronOr.Name = "ironOre"
    IronOr.Value = 0 -- As example

    local Stone = Instance.new("NumberValue",folder)
    Stone.Name = "Stone"
    Stone.Value = 0

    local AluminumOr = Instance.new("NumberValue",folder)
    AluminumOr.Name = "AluminumOre"
    AluminumOr.Value = 0

    local GoldOr = Instance.new("NumberValue",folder)
    GoldOr.Name = "GoldOre"
    GoldOr.Value = 0

    local SilverOr = Instance.new("NumberValue",folder)
    SilverOr.Name = "SilverOre"
    SilverOr.Value = 0

    local UraniumOr = Instance.new("NumberValue",folder)
    UraniumOr.Name = "UraniumOre"
    UraniumOr.Value = 0

    local Iron = Instance.new("NumberValue",folder)
    Iron.Name = "Iron"
    Iron.Value = 0

    local Ice = Instance.new("NumberValue",folder)
    Ice.Name = "Ice"
    Ice.Value = 0

    local Coal = Instance.new("NumberValue",folder)
    Coal.Name = "Coal"
    Coal.Value = 0

    local Gold = Instance.new("NumberValue",folder)
    Gold.Name = "Gold"
    Gold.Value = 0

    local Aluminum = Instance.new("NumberValue",folder)
    Aluminum.Name = "Aluminum"
    Aluminum.Value = 0

    local Silver = Instance.new("NumberValue",folder)
    Silver.Name = "Silver"
    Silver.Value = 0

    local Uranium = Instance.new("NumberValue",folder)
    Uranium.Name = "Uranium"
    Uranium.Value = 0

    if success and data then
        IronOr.Value = data[1]
        Stone.Value = data[2]
        GoldOr.Value = data[3]
        SilverOr.Value = data[4]
        UraniumOr.Value = data[5]
        Coal.Value = data[6]
        Ice.Value = data[7]
        Aluminum.Value = data[8]
        Gold.Value = data[9]
        Iron.Value = data[10]
        Uranium.Value = data[11]
        Silver.Value = data[12]
    else
        warn(errormessage)
    end

end)

game.Players.PlayerRemoving:Connect(function(plr)
    local key = plr.UserId.."Key2"

    local data = {
        plr.data.ironOre.Value,
        plr.data.Stone.Value,
        plr.data.GoldOre.Value,
        plr.data.SilverOre.Value,
        plr.data.UraniumOre.Value,
        plr.data.Coal.Value,
        plr.data.Ice.Value,
        plr.data.Aluminum.Value,
        plr.data.Gold.Value,
        plr.data.Iron.Value,
        plr.data.Uranium.Value,
        plr.data.Silver.Value
    }

    local success, err = pcall(function()
        DataStore:SetAsync(key, data)
    end)

end) 

game:BindToClose(function()
    if RunService:IsStudio() then return end
    for _, plr in ipairs(game.Players:GetPlayers()) do
        local key = plr.UserId.."Key2"

        local data = {
            plr.data.ironOre.Value,
            plr.data.Stone.Value,
            plr.data.GoldOre.Value,
            plr.data.SilverOre.Value,
            plr.data.UraniumOre.Value,
            plr.data.Coal.Value,
            plr.data.Ice.Value,
            plr.data.Aluminum.Value,
            plr.data.Gold.Value,
            plr.data.Iron.Value,
            plr.data.Uranium.Value,
            plr.data.Silver.Value
        }

        local success, err = pcall(function()
            DataStore:SetAsync(key, data)
        end)
    end
end)

Answer this question