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

Tool saving script fails to load/save data or clone tools into the backpack. What's wrong? [FIXED]

Asked by 4 years ago
Edited 4 years ago

Script maybe doesn't successfully save data. It loads it in too (Maybe this is where it goes wrong), but the tools do not clone into the backpack. What is the problem?

local dataStoreService = game:GetService("DataStoreService")
local toolStore = dataStoreService:GetDataStore("TOOLS")
--local inventoryStore = dataStoreService:GetDataStore("GuiInventory")
--local backpackstore = dataStoreService:GetDataStore("Backpack")
game.Players.PlayerAdded:Connect(function(player)
    local data = nil
    local sucess, errormessage = pcall(function()
        data = toolStore:GetAsync(player.UserId)
    end)
    if sucess then
        if data[#data] then
            for i,v in pairs(data) do
                local tool = game:WaitForChild("ReplicatedStorage").Tools:FindFirstChild(data[i])
                print(tool, data[i])
                if tool then
                    tool:Clone().Parent = player.Backpack
                end
            end
        end
    else
        warn("Datastore error!")
        error(errormessage)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local toolsToSave = {}
    for key, object in pairs(player.Backpack:GetChildren()) do
        table.insert(toolsToSave, object.Name)
    end
    local sucess, errormessage = pcall(function()
        toolStore:SetAsync(player.UserId, toolsToSave)
    end)
    if sucess then
        print(" ToolData Sucess!")
        print(player.Name .. ",s data has been saved, its ", game:GetService("HttpService"):JSONEncode(toolsToSave))
    else
        warn("Datastore error!")
        error(errormessage)
    end
end)
0
does something errors? Leamir 3138 — 4y
0
No, no errors R_LabradorRetriever 198 — 4y
0
What does it prints on line 14? Leamir 3138 — 4y
0
lemme see R_LabradorRetriever 198 — 4y
View all comments (4 more)
0
nil R_LabradorRetriever 198 — 4y
0
it's not saving correctly Leamir 3138 — 4y
0
Are you testing on studio or on real game? Depending on your game configuration datastores don't work on Roblox Studio Leamir 3138 — 4y
0
oof, im testing on studio R_LabradorRetriever 198 — 4y

Answer this question