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

Why isn't datastore saving correctly?

Asked by
Despayr 505 Moderation Voter
4 years ago

Ok, so here is my datastore. As you can see, it is a table of values that are saved.

When I change one thing in the character creator, it saves just fine, but if I customise more than a couple of things, it just refuses to save.

I had used a while wait do loop for every few seconds, and it was fine, but I want to refrain from doing so, otherwise the player will have to wait a minute just for the changes to save.

local Defaults = {

    Stats = {Level = 1, Experience = 0, Health = 0, Melee = 0, Weapon = 0, Fruit = 0, Ability = "None"};

    Inventory = {
        Weapons = {};
        Fruits = {};
        KeyItems = {};
        Other = {};
    };


    Appearance = {
        Hair = "Hair1";
        Shirt = "Short";
        Pants = "Short";
        Shoes = "Sandals";
        Tone = "Pastel Brown";
        HairPrimary = {0, 0, 0};
        ShirtPrimary = {0, 0, 0};
        ShirtSecondary = {0, 0, 0};
        PantsPrimary = {0, 0, 0};
        PantsSecondary = {0, 0, 0};
        ShoesPrimary = {0, 0, 0};
    };

    Codes = {};
    Equipment = {};
}


local datalist = {}

local store = game:GetService("DataStoreService")
local getstore = store:GetDataStore("Playerstats1")
local runservice = game:GetService("RunService")

local writedata = function(key, savedtable, newdata)

    if newdata.dataloaded == nil then return end
    if newdata.dataloaded == true then

        local succeed, errored = pcall(function()
            getstore:SetAsync(key, savedtable)
        end)
        if succeed then
            print("New Data saved successfully")
        else
            warn("Newdata has not been saved. Reason: "..errored)
        end


    end
end



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

    datalist[plr.UserId] = {}
    local newdata = datalist[plr.UserId]
    newdata.dataloaded = false

    local key = plr.UserId
    local savedtable

    local success, errormessage = pcall(function()
        savedtable = getstore:GetAsync(key)
    end)

    if success then

        if savedtable ~= nil then
            newdata.dataloaded = true
            newdata.mydata = savedtable

        else
            --new  player
            savedtable = Defaults
            newdata.mydata = savedtable
            newdata.dataloaded = true


            writedata(key, savedtable, newdata)
        end

    else    
        --failed to load data
        newdata.dataloaded = false
        wait()
        plr:Kick("Failed to load data. Rejoin to try again.")
    end


    coroutine.resume(coroutine.create(function()
        local remotes = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvents")
        local loadcharacterdata = remotes:WaitForChild("LoadCharacterData")
        loadcharacterdata:Fire(plr, savedtable, "Completely")--creates avatar using given data
    end))
end)

local updatedata = function(plr)

    local key = plr.UserId
    local newdata = datalist[plr.UserId]
-----------------------------------------------------------
    if newdata.dataloaded == nil or newdata.dataloaded == false then
        plr:Kick()
    else
        print("player has data")
    end
----------------------------------------------------------- 
    local savingdata = newdata.mydata
    if not savingdata then
        plr:Kick()
    end

    local successfully, warningmessage = pcall(function()
        getstore:SetAsync(key, savingdata)
    end)

    if successfully then
        --data successfully saved
        print("save successful")    
    else
        warn("warning, data could not be saved! Retrying!")
        local retry_count = 0
        while retry_count <6 do
            wait(60)
            local success, errormessage = pcall(function()
                getstore:SetAsync(key, savingdata)
            end)
            if success then
                break
            else
                retry_count = retry_count + 1
            end
        end
    end

end


game:GetService("Players").PlayerRemoving:Connect(updatedata)

game:BindToClose(function()
    if runservice:IsStudio() then
        print("is studio")
        return
    else
        if not runservice:IsStudio() then
            print("not in studio")
            for i, v in pairs(game:GetService("Players"):GetPlayers()) do
                updatedata(v)
            end
        end
    end
end)


local remotes = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvents")
local loadcharacterdata = remotes:WaitForChild("LoadCharacterData")
local sethair = remotes:WaitForChild("SetHair")
local setshirt = remotes:WaitForChild("SetShirt")
local setpants = remotes:WaitForChild("SetPants")
local setshoes = remotes:WaitForChild("SetShoes")
local settone = remotes:WaitForChild("SetTone")
local sethaircolour = remotes:WaitForChild("SetHairColour")
local setshirtcolour = remotes:WaitForChild("SetShirtColour")
local setpantscolour = remotes:WaitForChild("SetPantsColour")
local setshoescolour = remotes:WaitForChild("SetShoesColour")

sethair.Event:Connect(function(plr, id)
    local newdata = datalist[plr.UserId]
    if not newdata then return end

    local mydata = newdata.mydata
    if not mydata then return end

    mydata.Appearance.Hair = tostring(id)
    loadcharacterdata:Fire(plr, mydata, "Hair")--creates avatar using given data
end)

setshirt.Event:Connect(function(plr, id)
    local newdata = datalist[plr.UserId]
    if not newdata then return end

    local mydata = newdata.mydata
    if not mydata then return end

    mydata.Appearance.Shirt = tostring(id)
    loadcharacterdata:Fire(plr, mydata, "Shirt")--creates avatar using given data
end)

setpants.Event:Connect(function(plr, id)
    local newdata = datalist[plr.UserId]
    if not newdata then return end

    local mydata = newdata.mydata
    if not mydata then return end

    mydata.Appearance.Pants = tostring(id)
    loadcharacterdata:Fire(plr, mydata, "Pants")--creates avatar using given data
end)

setshoes.Event:Connect(function(plr, id)
    local newdata = datalist[plr.UserId]
    if not newdata then return end

    local mydata = newdata.mydata
    if not mydata then return end

    mydata.Appearance.Shoes = tostring(id)
    loadcharacterdata:Fire(plr, mydata, "Shoes")--creates avatar using given data
end)

settone.Event:Connect(function(plr, id)
    local newdata = datalist[plr.UserId]
    if not newdata then return end

    local mydata = newdata.mydata
    if not mydata then return end

    mydata.Appearance.Tone = tostring(id)
    loadcharacterdata:Fire(plr, mydata, "Tone") --creates avatar using given data
end)

sethaircolour.Event:Connect(function(plr, h, s, value)
    local newdata = datalist[plr.UserId]
    if not newdata then return end

    local mydata = newdata.mydata
    if not mydata then return end

    mydata.Appearance.HairPrimary[1] = tonumber(h)
    mydata.Appearance.HairPrimary[2] = tonumber(s)
    mydata.Appearance.HairPrimary[3] = tonumber(value)
end)

setshirtcolour.Event:Connect(function(plr, h, s, value)
    local newdata = datalist[plr.UserId]
    if not newdata then return end

    local mydata = newdata.mydata
    if not mydata then return end

    mydata.Appearance.ShirtPrimary[1] = tonumber(h)
    mydata.Appearance.ShirtPrimary[2] = tonumber(s)
    mydata.Appearance.ShirtPrimary[3] = tonumber(value)
end)

setpantscolour.Event:Connect(function(plr, h, s, value)
    local newdata = datalist[plr.UserId]
    if not newdata then return end

    local mydata = newdata.mydata
    if not mydata then return end

    mydata.Appearance.PantsPrimary[1] = tonumber(h)
    mydata.Appearance.PantsPrimary[2] = tonumber(s)
    mydata.Appearance.PantsPrimary[3] = tonumber(value)
end)

setshoescolour.Event:Connect(function(plr, h, s, value)
    local newdata = datalist[plr.UserId]
    if not newdata then return end

    local mydata = newdata.mydata
    if not mydata then return end

    mydata.Appearance.ShoesPrimary[1] = tonumber(h)
    mydata.Appearance.ShoesPrimary[2] = tonumber(s)
    mydata.Appearance.ShoesPrimary[3] = tonumber(value)
end)

I'm looking for answers as to why this could be happening.

0
DAYUMM BOI Aiden_12114 79 — 4y
0
pretty much xD Despayr 505 — 4y

Answer this question