Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i fix this data item saver?

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?

3 comments

0

im trying to saver items.... and i don't know how to.. i just don't get it...

0

Are there errors? Do you have the item in a folder named Inventory in ServerStorage and in the workspace?

1 answer

✓ Accepted answer

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.

2 comments

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.

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.