Untitled

mail@pastecode.io avatar
unknown
lua
2 years ago
1.8 kB
4
Indexable
Never
local PlayerData = game:GetService("DataStoreService"):GetDataStore("ToolsDataV3")
-- Gets the datastore service then gets the datastore

game.Players.PlayerAdded:connect(function(Player)
	local Part = Instance.new("Part", workspace)
		Part.BrickColor = BrickColor.new("Bright purple")
	local Key = "user_" ..Player.userId
	local Data = PlayerData:GetAsync(Key)
	-- get the players key and uses it to get the players data	
	
	if Data ~= nil then
		local Part = Instance.new("Part", workspace)
		Part.BrickColor = BrickColor.new("Bright red")
		print(Data)
		-- if there is some data
		for _, Tool in pairs (Data) do
			-- inserts the data (tools) into the players backpack
			print(Tool)
			local Part = Instance.new("Part", workspace)
			Part.BrickColor = BrickColor.new("Bright blue")
			for _, Item in pairs (game:GetService("ReplicatedStorage"):GetChildren()) do
				if Item.Name == Tool then
					Item:Clone().Parent = Player:WaitForChild("Backpack")
				end
			end
			
		end
	end
	
end)

game.Players.PlayerRemoving:connect(function(Player)
	local CurrentTools = {}
	
	if Player and Player.Character then
		if Player.Character:FindFirstChild("Humanoid") then
			Player.Character.Humanoid:UnequipTools()
			-- This unequips any tools the player has equipped
		end
	end
	
	for _, BackpackTool in pairs (Player:WaitForChild("Backpack"):GetChildren()) do
		-- This checks the players backpack for all tools
		if BackpackTool:IsA "Tool" then
			-- If the item in the backpack is a tool it inserts the name of it into the table
			table.insert(CurrentTools, BackpackTool.Name)
		end
	end
	
	
	local Key = "user_" ..Player.userId
	local UpdateData = PlayerData:SetAsync(Key, CurrentTools)
	-- Gets the players key and updates the datastore with the new table of the tools
end)