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

Why the saved tools are not being saved/loaded?

Asked by 9 years ago

I have a script were it saves the leaderstats and Tools. The script saves and loads the leaderstats, but it doesn't save/load the Tools. I don't know if it's not saving or not loading. Any help? 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"):GetChildren()
    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
        --Get and set RPG Status
        xp.Value = savedData[1]
        level.Value = savedData[2]
        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.Name).SG == 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) do
            if v.Name ~= "ClassicSlingshot" then
                table.insert(valuesToSave,index,v.Name)
                index = index + 1
            end
        end
        --Store values
        DataStore:SetAsync(key,valuesToSave)
    end
    -- When the player is leaving, save status
    game.Players.PlayerRemoving:connect(function(player)
        --Set values
        local valuesToSave = {xp.Value,level.Value,c.Value}
        local index = 4
        for i, v in pairs (Backpack) do
            if v.Name ~= "ClassicSlingshot" then
                table.insert(valuesToSave,index,v.Name)
                index = index + 1
            end
        end
        --Store values
        DataStore:SetAsync(key,valuesToSave)
    end)
end)



Answer this question