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

This Code Saves Data, but it doesn't. Can anyone help?

Asked by 3 years ago
Edited 3 years ago

there are no errors and the game just wont save data

local dataStore = game:GetService("DataStoreService"):GetDataStore("MyMiningData")
local capacityEvent = game.ReplicatedStorage:WaitForChild("UICapacityEvent")
local boughtRequest = game.ReplicatedStorage:WaitForChild("BoughtRequest")
_G.boughtArray = {}
local debugMode =true

function onSpawn(character)
    local player = game.Players:GetPlayerFromCharacter(character)
    local pickaxe = player:WaitForChild("Pickaxe").Value
    local backpack = player:WaitForChild("BackpackItem").Value
    local newPickaxe = game.ReplicatedStorage:FindFirstChild(pickaxe):Clone()
    newPickaxe.Parent = player.Backpack
    local newBackpack = game.ReplicatedStorage:FindFirstChild(backpack):Clone()
    player.Character.Humanoid:AddAccessory(newBackpack)
    local capacity = newBackpack.Capacity.Value
    capacityEvent:FireClient(player, capacity)
end
function onPlayerEntered(player)
    local initKey = "user_" .. player.userId .. "_init"
    local coinsKey = "user_" .. player.userId .. "_coins"
    local minedKey = "user_" .. player.userId .. "_mined"
    local invenValKey = "user_" .. player.userId .. "_invenVal"
    local invenCountKey = "user_" .. player.userId .. "_invenCount"
    local backpackKey = "user_" .. player.userId .. "_backpack"
    local pickaxeKey = "user_" .. player.userId .. "_pickaxe"
    local boughtKey = "user_" .. player.userId .. "_bought"
    local rebirthKey = "user_" .. player.userId .. "_rebirth"
    local rebirthCKey = "user_" .. player.userId .. "_rebrithC"
    if (dataStore:GetAsync(initKey) == nil or debugMode == true) then
        dataStore:SetAsync(initKey, true)
        dataStore:SetAsync(coinsKey, 0)
        dataStore:SetAsync(minedKey, 0)
        dataStore:SetAsync(invenValKey, 0)
        dataStore:SetAsync(invenCountKey, 0)
        dataStore:SetAsync(backpackKey, "BackpackBasic")
        dataStore:SetAsync(pickaxeKey, "Basic Pickaxe")
        dataStore:SetAsync(boughtKey, {})
        dataStore:SetAsync(rebirthKey, 0)
        dataStore:SetAsync(rebirthCKey, 10000000)
    end
    local init = dataStore:GetAsync(initKey)
    local mined = dataStore:GetAsync(minedKey)
    local coins = dataStore:GetAsync(coinsKey)
    local invenVal = dataStore:GetAsync(invenValKey)
    local invenCount = dataStore:GetAsync(invenCountKey)
    local backpack = dataStore:GetAsync(backpackKey)
    local pickaxe = dataStore:GetAsync(pickaxeKey)
    local bought = dataStore:GetAsync(boughtKey)
    local rebirth = dataStore:GetAsync(rebirthKey)
    local rebirthC = dataStore:GetAsync(rebirthCKey)
    _G.boughtArray[player.userId] = bought
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    local minedBoard = Instance.new("IntValue")
    minedBoard.Name = "Mined"
    minedBoard.Value = mined
    minedBoard.Parent = leaderstats
    local coinsBoard = Instance.new("IntValue")
    coinsBoard.Name = "Coins"
    coinsBoard.Value = coins
    coinsBoard.Parent = leaderstats
    local rebirthBoard = Instance.new("IntValue")
    rebirthBoard.Name = "Rebirths"
    rebirthBoard.Value = rebirth
    rebirthBoard.Parent = leaderstats
    local rebirthCBoard = Instance.new("IntValue")
    rebirthCBoard.Name = "RebirthCost"
    rebirthCBoard.Value = rebirthC
    rebirthCBoard.Parent = player
    local value = Instance.new("IntValue")
    value.Name = "InventoryValue"
    value.Value = invenVal
    value.Parent = player
    local count = Instance.new("IntValue")
    count.Name = "InventoryCount"
    count.Value = invenCount
    count.Parent = player
    local bpObject = Instance.new("StringValue")
    bpObject.Name = "BackpackItem"
    bpObject.Value = backpack
    bpObject.Parent = player
    local pkObject = Instance.new("StringValue")
    pkObject.Name = "Pickaxe"
    pkObject.Value = pickaxe
    pkObject.Parent = player
    local inMine = Instance.new("BoolValue")
    inMine.Name = "InMine"
    inMine.Value = false
    inMine.Parent = player
    onSpawn(player.Character)
    player.CharacterAdded:Connect(onSpawn)
    print("Data correctly loaded for " .. player.Name)
end
function onPlayerLeaving(player)
    print("Starting to save player progress")
    local userID = player.UserId
    print("Loading Leaderstats")
    local stats = player:WaitForChild("leaderstats")
    print("Finished Loading")
    local mined = stats.Mined.Value
    local coins = stats.Coins.Value
    local rebirth = stats.Rebirths.Value
    local invenVal = player.InventoryValue.Value
    local invenCount = player.InventoryCount.Value
    local backpack = player.BackpackItem.Value
    local pickaxe = player.Pickaxe.Value
    local rebirthC = player.RebirthCost.Value
    local bought = _G.boughtArray[player.userId]
    print("Blocks Mined: " .. mined ..  "Coins: " .. coins)
    local minedKey = "user_" .. player.userId .. "_mined"
    local coinsKey = "user_" .. player.userId .. "_coins"
    local rebirthKey = "user_" .. player.userId .. "_rebirth"
    local rebirthMKey = "user_" .. player.userId .. "_rebirthM"
    local invenValKey = "user_" .. player.userId .. "_invenVal"
    local invenCountKey = "user_" .. player.userId .. "_invenCount"
    local backpackKey = "user_" .. player.userId .. "_backpack"
    local pickaxeKey = "user_" .. player.userId .. "_pickaxe"
    local boughtKey = "user_" .. player.userId .. "_bought"
    local rebirthCKey = "user_" .. player.userId .. "_rebrithC"
    dataStore:SetAsync(minedKey, mined)
    dataStore:SetAsync(coinsKey, coins)
    dataStore:SetAsync(rebirthKey, rebirth)
    dataStore:SetAsync(invenValKey, invenVal)
    dataStore:SetAsync(invenCountKey, invenCount)
    dataStore:SetAsync(backpackKey, backpack)
    dataStore:SetAsync(pickaxeKey, pickaxe)
    dataStore:SetAsync(boughtKey, bought)
    dataStore:SetAsync(rebirthCKey, rebirthC)
    print("Player Saved Game")
end
function onBoughtRequest(player)
    while (_G.boughtArray[player.userId] == nil) do
        wait(0.1)
    end
    return _G.boughtArray[player.userId]
end
game.Players.PlayerAdded:Connect(onPlayerEntered)
game.Players.PlayerRemoving:Connect(onPlayerLeaving)
boughtRequest.OnServerInvoke = onBoughtRequest
function Rebirth(player)
    local coins = player.leaderstats:FindFirstChild("Coins")
    local rebirth = player.leaderstats:FindFirstChild("Rebirths")
    local cost = player:FindFirstChild("RebirthCost")
    local invenVal = player:FindFirstChild("InventoryValue")
    local invenCount = player:FindFirstChild("InventoryCount")
    local bpObject = player:FindFirstChild("BackpackItem")
    local pkObject = player:FindFirstChild("Pickaxe")
    local stats = player.leaderstats
    local pickaxe = game.ReplicatedStorage:FindFirstChild("Basic Pickaxe")
    local backpack = game.ReplicatedStorage:FindFirstChild("BackpackBasic")
    local playerGui = player:WaitForChild("PlayerGui")
    local bought = _G.boughtArray
    if coins.Value >= cost.Value then
        coins.Value = 0
        cost.Value = cost.Value + 10000000
        rebirth.Value = rebirth.Value + 1
        invenVal = 0
        invenCount = 0
        for k in pairs(bought) do
            bought[k] = {}
        end
        for _, buttons in pairs(playerGui.MainGui.ShopFrame:GetDescendants()) do
            if buttons:IsA("TextButton") then
                buttons.BackgroundColor3 = Color3.fromRGB(92, 195, 37)
            end
        end
        bpObject = "BackpackBasic"
        pkObject = "Basic Pickaxe"
        local newItem1 = pickaxe:Clone()
        local newItem2 = backpack:Clone()
        player.Character.Humanoid:UnequipTools()
        player:FindFirstChild(player.Pickaxe.Value, true):Destroy()
        newItem1.Parent = player.Backpack
        player.Pickaxe.Value = newItem1.Name
        player.Character:FindFirstChild(player.BackpackItem.Value):Destroy()
        player.Character.Humanoid:AddAccessory(newItem2)
        player.BackpackItem.Value = newItem2.Name
        capacityEvent:FireClient(player, newItem2.Capacity.Value)
        return true
    end
    return false
end
game.ReplicatedStorage.Rebirth.OnServerInvoke = Rebirth

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

When you save the data, embed it in a pcall. For example:

game.Players.PlayerAdded:connect(function(player)
    local success, err = pcall(function()
        datastore:SetAsync(key,value)
    end)
    if success then
    print("Success!")
    elseif not success and err then
    print("Error: "..err)
    end
end)


Ad

Answer this question