Tools DataStore

I made a system to save the player’s tools when he exits the game. They are also returned to the player when they die or reset. It also has a function to give starting items, in case the player has never played. (Put the script in “ServerScriptService”)

Any problems, please comment.

local PlayersService = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

local DataStoreService = game:GetService("DataStoreService")
local GameData = DataStoreService:GetDataStore("Tools")

local Tools = ServerStorage:WaitForChild("Tools") -- Your tools folder

function StarterPack(Player) -- Starting items if the player has never played
	Tools:WaitForChild("Sword"):Clone().Parent = Player:WaitForChild("Backpack")
end

PlayersService.PlayerAdded:Connect(function(Player)
	local GetData = true
	local LastTools = {}
	
	Player.CharacterAdded:Connect(function(Character)
		if GetData then
			GetData = false
			
			local Backpack = Player:WaitForChild("Backpack")
			
			local sucess, Data = pcall(function()
				return GameData:GetAsync(Player.UserId.."-Tools")
			end)
			
			if sucess and Data ~= nil then
				print(Data)
				for index, Tool in ipairs(Data) do
					Tools[Tool]:Clone().Parent = Backpack
				end
			else
				StarterPack(Player)
			end
		else
			local Backpack = Player:WaitForChild("Backpack")

			for index, Tool in pairs(LastTools) do
				Tools[Tool]:Clone().Parent = Backpack
			end
			
			LastTools = {}
		end
	end)
	
	Player.CharacterRemoving:Connect(function(Character)
		Character.Humanoid:UnequipTools()
		
		local Backpack = Player:WaitForChild("Backpack")
		
		for index, Tool in pairs(Backpack:GetChildren()) do
			table.insert(LastTools, Tool.Name)
		end
	end)
end)

PlayersService.PlayerRemoving:Connect(function(Player)
	local Backpack = Player:WaitForChild("Backpack")
	
	local ToolsData = {}
	
	for index, Tool in pairs(Backpack:GetChildren()) do
		table.insert(ToolsData, Tool.Name)
	end
	
	local sucess, erro = pcall(function()
		GameData:SetAsync(Player.UserId.."-Tools", ToolsData)
	end)
end)

game:BindToClose(function()
	for index, Player in pairs(PlayersService:GetPlayers()) do
		local Backpack = Player:WaitForChild("Backpack")

		local ToolsData = {}

		for index, Tool in pairs(Backpack:GetChildren()) do
			table.insert(ToolsData, Tool.Name)
		end

		local sucess, erro = pcall(function()
			GameData:SetAsync(Player.UserId.."-Tools", ToolsData)
		end)
	end
end)
1 Like

create a model in roblox.com so I can get it without creating the script, this is a input test on length

This resource is not clearly explained enough and not detailed.

  • What is the purpose of this script?
  • What are the instructions to install your resource?
  • How can we utilize and use this resource effectively? All you did in this post is you only told us the purpose and how to install this resource. How are we suppose to use it without clear instructions?
  • Why do you think your resource should be used by other developers? What benefits could they get from this? Why do you think it’s good?

Overall, this resource does sound promising, but you have failed to provide details and explanation in order for developers to use it. You should edit your post by including all the necessary information if you want your script to be used by other developers.

1 Like