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

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

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?

2 comments

0

you need to wait for the character to be added and then clone the tool to the backpack

0

also userId is deprecated, use UserId

1 answer

✓ Accepted answer
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

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