#tool saving dosent work

1 messages · Page 1 of 1 (latest)

opaque relic
#
local datastoreService = game:GetService("DataStoreService")
local playerTools = datastoreService:GetDataStore("PlayerTools")
local toolManager = game.ReplicatedStorage:WaitForChild("Tools")

local function playerjoined(plr)
    repeat task.wait()
        
    until plr.Character
    task.wait(3)
    
    local succes, inventory = pcall(function()
        return playerTools:GetAsync(plr.UserId)
    end)
    
    for i, name in pairs(inventory) do
        local tool = toolManager:FindFirstChild(name)
        if tool then
            local toolClone = tool:Clone()
            toolClone.Parent = plr.Backpack
        end
    end
end

local function playerleft(plr)
    local backpack = plr.Backpack
    local char = plr.Character
    local tools = {}
    
    if char  then
        for i, tool in pairs(char:GetChildren()) do
            if tool:IsA("Tool") then
                table.insert(tools,tool.Name)
            end
        end
    end
    
    if backpack then
        for i, tool in pairs(backpack:GetChildren()) do
            if tool:IsA("Tool") then
                table.insert(tools,tool.Name)
            end
        end
    end
    
    local succes, err = pcall(function()
        return playerTools:SetAsync(plr.UserId,tools)
    end)
    
    if not succes then
        warn(err)
    end
end


game.Players.PlayerAdded:Connect(function(plr)
    playerjoined(plr)
end)

game.Players.PlayerRemoving:Connect(function(plr)
    playerleft(plr)
end)```
rough silo
#

you

#

cant

#

save

#

tables

#

in

#

datastores,

#

only

#

text

rich rover
#

you can basically save whatever you want just not userdata (Instances...)

rough silo
#
local datastoreService = game:GetService("DataStoreService")
local playerTools = datastoreService:GetDataStore("PlayerTools")
local toolManager = game.ReplicatedStorage:WaitForChild("Tools")

local function playerjoined(plr)
    repeat task.wait()
        
    until plr.Character
    task.wait(3)
    
    local succes, inventory = pcall(function()
        return string.split(playerTools:GetAsync(plr.UserId),":::")
    end)
    
    for i, name in inventory do -- pairs is so 2018
        local tool = toolManager:FindFirstChild(name)
        if tool then
            local toolClone = tool:Clone()
            toolClone.Parent = plr.Backpack
        end
    end
end

local function playerleft(plr)
    local backpack = plr.Backpack
    local char = plr.Character
    local tools = ""
    
    if char  then
        for i, tool in char:GetChildren() do
            if tool:IsA("Tool") then
                tools = tools .. tool.Name .. ":::"
            end
        end
    end
    
    if backpack then
        for i, tool in backpack:GetChildren() do
            if tool:IsA("Tool") then
                tools = tools .. tool.Name .. ":::"
            end
        end
    end
    
    local succes, err = pcall(function()
        return playerTools:SetAsync(plr.UserId,tools)
    end)
    
    if not succes then
        warn(err)
    end
end


game.Players.PlayerAdded:Connect(function(plr)
    playerjoined(plr)
end)

game.Players.PlayerRemoving:Connect(function(plr)
    playerleft(plr)
end)
rich rover
#

yep

rough silo
#

meaning only strings

#

no tables

rough silo
rich rover
#

that is not correct.

rich rover
#

as you can see I saved a table, the only thing is that this get encoded in json and then decoded

rough silo
opaque relic
#

honestly idk what to do

teal sorrel
#

if your having issues uh

#

you need to just go through the needed properties of the tool

#

you might only need name

#

and then you can just

#
local tools = {};
for i, v in ipairs(player.Backpack:GetChildren()) --[[ipairs for order]] do
  table.insert(tools, v.Name);
end```
#

rewrite your original code since you do smth like this

#

its prob an issue somewhere in your code so do a ton of debug prints

#

or smth

#

idk

rough silo
#

ahh

#

the player character cant be accessed through player removing

rough silo
#
local datastoreService = game:GetService("DataStoreService")
local playerTools = datastoreService:GetDataStore("PlayerTools")
local toolManager = game.ReplicatedStorage:WaitForChild("Tools")

local function playerjoined(plr)
    repeat task.wait()
        
    until plr.Character
    task.wait(3)
    
    local succes, inventory = pcall(function()
        return string.split(playerTools:GetAsync(plr.UserId),":::")
    end)
    
    for i, name in inventory do -- pairs is so 2018
        local tool = toolManager:FindFirstChild(name)
        if tool then
            local toolClone = tool:Clone()
            toolClone.Parent = plr.Backpack
        end
    end
end

local function playerleft(plr,char)
    local backpack = plr.Backpack
    local tools = ""
    
    if char  then
        for i, tool in char:GetChildren() do
            if tool:IsA("Tool") then
                tools = tools .. tool.Name .. ":::"
            end
        end
    end
    
    if backpack then
        for i, tool in backpack:GetChildren() do
            if tool:IsA("Tool") then
                tools = tools .. tool.Name .. ":::"
            end
        end
    end
    
    local succes, err = pcall(function()
        return playerTools:SetAsync(plr.UserId,tools)
    end)
    
    if not succes then
        warn(err)
    end
end


game.Players.PlayerAdded:Connect(function(plr)
    playerjoined(plr)
    plr.CharacterRemoving:Connect(function(char)
      playerleft(plr,char)
    end)
end)
#

this must work

storm wren
#

game:bindtoclose

opaque relic
#

dosent work, I also get this error

storm wren
opaque relic
#

honestly it feels not possible to save tools in any way