Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Save script can't parent the saved tools to the correct place?

Asked by 7 days ago
local service = game:GetService("DataStoreService")
local datastore = service:GetDataStore("hleol")

game.Players.PlayerAdded:Connect(function(Player)
    if datastore:GetAsync(Player.userId) then
        for i, v in pairs(datastore:GetAsync(Player.userId)) do
            print(v)
            local tool = game.ServerStorage[v]:Clone()
            tool.Parent = Player.Backpack
        end
    end
end)



game.Players.PlayerRemoving:Connect(function(Player)
    local ToolsToSave = { }
    for i, v in pairs(Player.Backpack:GetChildren()) do
        table.insert(ToolsToSave, v.Name)
    end
    print("a")
    datastore:SetAsync(Player.userId, ToolsToSave)
end)

At line 9, it does not parent it to the backpack. I tried with workspace, and it worked. But backpack seem to not be broken. Any fix?

0
you need to wait for the character to be added and then clone the tool to the backpack Gameplayer365247v2 435 — 7d
0
also userId is deprecated, use UserId Gameplayer365247v2 435 — 7d

1 answer

Log in to vote
1
Answered by 7 days ago
Edited 7 days ago
local service = game:GetService("DataStoreService")
local datastore = service:GetDataStore("hleol")

game.Players.PlayerAdded:Connect(function(Player)
    if datastore:GetAsync(Player.UserId) then
        for i, v in pairs(datastore:GetAsync(Player.userId)) do
            print(v)
            Player.CharacterAdded:Connect(function()
            local tool = game.ServerStorage[v]:Clone()
            tool.Parent = Player.Backpack
           end)
        end
    end
end)



game.Players.PlayerRemoving:Connect(function(Player)
    local ToolsToSave = { }
    for i, v in pairs(Player.Backpack:GetChildren()) do
        table.insert(ToolsToSave, v.Name)
    end
    print("a")
    datastore:SetAsync(Player.UserId, ToolsToSave)
end)

this will wait for the character and then clone the tool, this needs to be done since when the character gets added the players backpack gets deleted

Ad

Answer this question