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

How do i fix this data item saver?

Asked by 6 years ago

here's the code:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Inventory") -- Separate variable

local function Load(plr)
    local key = "plr-"..plr.UserId -- key variable (The way I do it)
    local savedData -- Will be set below

    local success, err = pcall(function()
        -- This makes sure that the script will keep going if it errors
        savedData = DataStore:GetAsync(key) -- GetAsync()
    end  

    -- if datastore failed to read data
    if not success then
        warn("Failed to read data"..tostring(err))
        return -- this will return nil
    end

    -- if datastore succeeded 
    if savedData then
        -- if the player has data then load the data

        for i, v in pairs(savedData) do
            if v then
                local item = game.ServerStorage.Inventory:FindFirstChild(v):Clone()

                if item then
                    item.Parent = plr:WaitForChild("Backpack")
                end
            end
        end
    else
        -- If the player does not have data to be loaded

        local items = {}

        local itemsSaving = plr:WaitForChild("Backpack"):GetChildren()

        for i, v in pairs(itemsSaving) do
            if v then
                table.insert(items, v.Name)
            end
        end

        local success, err = pcall(function() -- protected call
            DataStore:SetAsync(key, items) -- SetAsync()
        end

        -- if overwriting data failed
        if not success then
            warn("Failed to overwrite data"..tostring(err))
            return
        end
    end
end

local function SaveData(plr)
    local key = "plr-"..plr.UserId -- Again just how I do it
    local items = {}

    local itemsSaving = plr:WaitForChild("Backpack"):GetChildren()

    for i, v in pairs(itemsSaving) do
        if v then
            table.insert(items, v.Name)
        end
    end

    local success, err = pcall(function()
        DataStore:SetAsync(key, items) -- SetAsync()
    end

    -- failed
    if not success then
        warn("Failed to overwrite data"..tostring(err))
        return
    end
end

-- Calls
game.Players.PlayerAdded:Connect(Load)
game.Players.PlayerRemoving:Connect(SaveData)

Can u test it guys? how doi fix this or rescript it?

0
im trying to saver items.... and i don't know how to.. i just don't get it... doomoxima21 17 — 6y
0
Are there errors? Do you have the item in a folder named Inventory in ServerStorage and in the workspace? ABK2017 406 — 6y

1 answer

Log in to vote
1
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
6 years ago

It is NOT possible to save objects to a DataStore. Instead, you could probably save the names of the tools, and when the player comes back, it would give them tools according to the names in their saved data.

0
He could do a for i,v loop in the player's tools and save them into a table, and then save the table in the datastore. That's what I do. wilsonsilva007 373 — 6y
0
Yeah, he'd have to do that with their names, not the actual tools (I'm sure you mean the names, I'm just being specific for him) Shawnyg 4330 — 6y
Ad

Answer this question