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

DataStore script not saving correctly?

Asked by
Vid_eo 126
5 years ago
Edited 5 years ago

I'm making a datastore that saves both money and gears. I started testing the datastore by seeing if it saved money, and it didn't save money. When the players had no save data, though, it set their money value to 520 like the script should do.

Basically my issue is that the script always sets money's value to 520, even if I change the money value to something like 1000 and leave the game. I want it to set the money value to 1000 when I leave the game, not 520. Here's my scipt:

local DataStoreService = game:GetService("DataStoreService")
local ds = DataStoreService:GetDataStore("ToolSave")
local ds2 = DataStoreService:GetDataStore("MoneySave")

game.Players.PlayerAdded:connect(function(Player) --When the player joins the game, load value.

    local Stats = Instance.new('Model')
    Stats.Name = "leaderstats"

    local Money = Instance.new('NumberValue',Stats)
    Money.Name = "Money"

    Stats.Parent = Player


    --load data
    local moneyData;
    local toolData;

    local CanSave = Instance.new('BoolValue', Player)

    CanSave.Name = "CanSaveData"
    CanSave.Value = true

    local DataFetchSuccess, ErrorMessage = pcall(function()
        moneyData = ds2:GetAsync(tostring(Player.UserId))
        toolData = ds:GetAsync(tostring(Player.UserId))
    end)
    if DataFetchSuccess then 
        if moneyData ~= nil then --This could be where the problem is
            Money.Value = moneyData
        else
            Money.Value = 520 -- Where money value is changed to 520
        end
        print(Money.Value)
        if toolData ~= nil then
            for i,v in pairs(toolData) do
                print(v)
                local tool = game.ServerStorage.Tools:FindFirstChild(v)
                if toolData then
                    toolData:Clone().Parent = Player:WaitForChild("Backpack")
                    toolData:Clone().Parent = Player:WaitForChild("StarterGear")
                end         
            end
        end
    else
        Player.CanSaveData.Value = false
        Player:Kick("Your data failed to load! Please rejoin.")
    end
end)

game.Players.PlayerRemoving:connect(function(Player) -- When the player leaves the game, data must save.
    if Player.CanSaveData.Value == false then return end - -[[There could also be a problem with saving the money--]]

    local moneyData = Player.leaderstast.Money
    local toolsToSave = {}

    for i,v in pairs (Player.Backpack:GetChildren()) do
        if v then
            table.insert(toolsToSave, v.Name)
        end
    end

    local DataWriteSuccess, ErrorMessage = pcall(function()
        ds:SetAsync(tostring(Player.UserId), toolsToSave)
        ds2:SetAsync(tostring(Player.UserId), moneyData)
    end)

    if not DataWriteSuccess then
        local Retry_Count = 0

        while Retry_Count < 6 do
            wait(60)
            local Succeed, Error = pcall(function()
                ds:SetAsync(tostring(Player.UserId), toolsToSave)
                ds2:SetAsync(tostring(Player.UserId), moneyData)
            end)
            if Succeed then break end
            Retry_Count = Retry_Count + 1
        end
    end
end)


Thank you!

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago
local DataStoreService = game:GetService("DataStoreService")
local ds = DataStoreService:GetDataStore("ToolSave")
local ds2 = DataStoreService:GetDataStore("MoneySave")

game:GetService("Players").PlayerAdded:Connect(function(Player) --When the player joins the game, load value.

    local Stats = Instance.new('Folder')
    Stats.Name = "leaderstats"

    local Money = Instance.new('NumberValue', Stats)
    Money.Name = "Money"

    Stats.Parent = Player


    --load data
    local moneyData;
    local toolData;

    local CanSave = Instance.new('BoolValue', Player)

    CanSave.Name = "CanSaveData"
    CanSave.Value = true

    local DataFetchSuccess, ErrorMessage = pcall(function()
        moneyData = ds2:GetAsync(tostring(Player.UserId))
        toolData = ds:GetAsync(tostring(Player.UserId))
    end)
    if DataFetchSuccess then 
        if moneyData ~= nil then --This could be where the problem is
            print("Money save found")
            Money.Value = moneyData
        else
        print("No money save found.")
            Money.Value = 520 -- Where money value is changed to 520
        end
        print(Money.Value)
        if toolData ~= nil then
            for i,v in pairs(toolData) do
                print(v)
                local tool = game.ServerStorage.Tools:FindFirstChild(v)
                if toolData then
                    toolData:Clone().Parent = Player:WaitForChild("Backpack")
                    toolData:Clone().Parent = Player:WaitForChild("StarterGear")
                end         
            end
        end
    else
        Player.CanSaveData.Value = false
        Player:Kick("Your data failed to load! Please rejoin.")
    end
end)

game:GetService("Players").PlayerRemoving:Connect(function(Player) -- When the player leaves the game, data must save.
    if Player.CanSaveData.Value == false then return end --There could also be a problem with saving the money

    local moneyData = Player.leaderstats.Money.Value
    local toolsToSave = {}

    print(MoneyData)
    print(toolsToSave)

    for i,v in pairs (Player.Backpack:GetChildren()) do
        if v then
            table.insert(toolsToSave, v.Name)
        end
    end

    local DataWriteSuccess, ErrorMessage = pcall(function()
    print("Saving "..Player.Name.."s stats")
        ds:SetAsync(tostring(Player.UserId), toolsToSave)
        ds2:SetAsync(tostring(Player.UserId), moneyData)
    end)

    if not DataWriteSuccess then
        local Retry_Count = 0

        while Retry_Count < 6 do
            wait(60)
            local Succeed, Error = pcall(function()
        print("Attempting to save "..Player.Name.."'s stats.")
                ds:SetAsync(tostring(Player.UserId), toolsToSave)
                ds2:SetAsync(tostring(Player.UserId), moneyData)
            end)
            if Succeed then print("Saved "..Player.Name.."'s stats") break end
            Retry_Count = Retry_Count + 1
        end
    end
end)


0
Yeah, I noticed that too and fixed it. It's still not working Vid_eo 126 — 5y
0
If I type print(moneyData) in the pcall function and right after it's declared as a variable, it shows nil. If I print it after the function, though, it just prints nothing. Vid_eo 126 — 5y
0
I edited the answer, try that SystemFormat 149 — 5y
0
Also, you typed "leaderstats" as "leaderstast" on line 55 SystemFormat 149 — 5y
View all comments (5 more)
0
So I'm supposed to load the money instance instead of the value? Or save the value and load the value? Vid_eo 126 — 5y
0
edited again SystemFormat 149 — 5y
0
Alright, I fixed that (I figured it out after looking at the script for a few minutes) & it's still not working for some reason. I'm using prints to try finding the issue, but I can't find it. Vid_eo 126 — 5y
0
I changed the answer to include a few other things. Try that code out and let me know the results. SystemFormat 149 — 5y
0
Thanks for your help! It works Vid_eo 126 — 5y
Ad

Answer this question