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

For some reason, my script is saving the wrong items, Help?

Asked by 9 years ago

I have a script that saves the tools and the leaderstats. It works, but there's a really weird problem. If i join my game, get different tools, leave and rejoin the game, some of the tools loaded are replaced with Healing Potions, and sometimes it has additional healing potions. For example:

Joined game

Gets Sword, Trowel and a Superball

Leaves game (When i leave the items that i just got are saved)

Joins game

**Loads ** up for me a Healing potion, Trowel, Healing potion

What is causing this weird problem? Script:

---------------
--[Variables]--
---------------

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


---------------
--[Task Area]--
---------------

game.Players.PlayerAdded:connect(function(newPlayer)
    --Creates variables
    local leaderstats = newPlayer:WaitForChild("leaderstats")
    local xp = leaderstats:WaitForChild("XP")
    local level = leaderstats:WaitForChild("Lvl")
    local c = leaderstats:WaitForChild("Tix")
    local Backpack = newPlayer:WaitForChild("Backpack")
    newPlayer:WaitForChild("StarterGear")

    --Creates user key
    local key = "user-"..newPlayer.userId

    --Get saved data
    local savedData = DataStore:GetAsync(key)


    --If player has data saved, load data. If not, save current data.
    if savedData then
        local s = ""
        for i, v in pairs (savedData) do
            s = s..v.."/"
        end
        print (s)
        --Get and set RPG Status
        level.Value = savedData[2]
        xp.Value = savedData[1]
        c.Value = savedData[3]
        --Get and set tools
        for i, v in pairs (savedData) do
            if i>3 and v ~= "ClassicSlingshot" then
                print(v)
                local clone = game.ServerStorage.Gears.Items:FindFirstChild(v):clone()
                clone.Parent = newPlayer.Backpack
                if game.ServerStorage.Gears.Items:FindFirstChild(v).SG.Value == true then
                    local clone2 = game.ServerStorage.Gears.Items:FindFirstChild(v):clone()
                    clone2.Parent = newPlayer.StarterGear
                end 
            end
        end

    else        
        --Set values
        local valuesToSave = {xp.Value,level.Value,c.Value}
        local index = 4
        for i, v in pairs (Backpack:GetChildren()) do
            if v.Name ~= "ClassicSlingshot" then
                table.insert(valuesToSave,index,v.Name)
                index = index + 1
            end
        end
        --Store values
        DataStore:SetAsync(key,valuesToSave)
    end
    local function SaveStatusFunction()
        --Set values
        local valuesToSave = {xp.Value,level.Value,c.Value}
        local index = 4

        for i, v in pairs (Backpack:GetChildren()) do
            if v.Name ~= "ClassicSlingshot" then
                table.insert(valuesToSave,index,v.Name)
                index = index + 1
            end
        end
        if newPlayer.Character ~= nil then
            for i, v in pairs(newPlayer.Character:GetChildren()) do
                if v.ClassName == "Tool" then
                    table.insert(valuesToSave,index,v.Name)
                    index = index + 1
                end
            end
        end
        --Store values
        DataStore:SetAsync(key,valuesToSave)
    end
    -- When the player is leaving, save status
    game.Players.PlayerRemoving:connect(SaveStatusFunction)

end)


0
I may have just learned something today. To save tables to Data Store, do you need to encode it with JSON? Validark 1580 — 9y

Answer this question