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

How to save Tools for each player?

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("mydatastore")

game.Players.PlayerAdded:Connect(function(plr)
	local data 
	local success, errorMessage = pcall(function()
		data = dataStore:GetAsync(plr.UserId.."tools")
		
	end)
	
	if data ~= nil then
		for _, toolName in pairs(data)do
			local tool = game.ReplicatedStorage.Weapons:FindFirstChild(toolName)
			if tool then
				local newTool = tool:Clone()
				newTool.Parent = plr.Backpack
				
				local newTool2 = tool:Clone()
				newTool2.Parent = plr.StarterGear
			end
		end
	end
	
end)

game.Players.PlayerRemoving:Connect(function(plr)
	plr.Character.Humanoid:UnequipTools()
	local toolsTable = {}
	 
	for _, tool in pairs(plr.Backpack:GetChildren())do
		if game.ReplicatedStorage.Weapons:FindFirstChild(tool.Name)then
			table.insert(toolsTable, tool.Name)
		end
	end
	
	local success, errorMessage = pcall(function()
		dataStore:SetAsync(plr.UserId .. "tools", toolsTable)
	end)
	
end)
 game:BindToClose(function()
	for _, plr in pairs(game.Players:GetPlayers())do
		local toolsTable = {}
	 
	for _, tool in pairs(plr.Backpack:GetChildren())do
		if game.ReplicatedStorage.Weapons:FindFirstChild(tool.Name)then
				table.insert(toolsTable, tool.Name)
				
		end
		end
		
	
	local success, errorMessage = pcall(function()
		dataStore:SetAsync(plr.UserId, toolsTable)
		end)
		
	end
end)

The error takes place in plr.Character.Humanoid:UnequipTools() (line 27), which does save the tools for me, but doesnt for other players. I need to Unequip the Tools because I noticed if the player has them equiped he loses that specific tool upon exit.

3 comments

0

The reason why it results in an error is because when the player leaves, their character is also gone

0

use a string value to input the tool name so when player rejoins the string value tells the game what tool they had if you make the string value save it should work but idk

0

Used character removing in the Players Added section instead, seems like it works

1 answer

Working Code boys

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("mydatastore")

game.Players.PlayerAdded:Connect(function(plr)
	
	local data 
	local success, errorMessage = pcall(function()
		data = dataStore:GetAsync(plr.UserId.."tools")
		
	end)
	
	if data ~= nil then
		for _, toolName in pairs(data)do
			local tool = game.ReplicatedStorage.Weapons:FindFirstChild(toolName)
			if tool then
				local newTool = tool:Clone()
				newTool.Parent = plr.Backpack
				
				local newTool2 = tool:Clone()
				newTool2.Parent = plr.StarterGear
			end
		end
	end
	plr.CharacterRemoving:Connect(function(Character)
		Character:WaitForChild("Humanoid"):UnequipTools()
		print("tools unequiped")
	end)
	
end)

game.Players.PlayerRemoving:Connect(function(plr)
	--plr.Character.Humanoid:UnequipTools()
	local toolsTable = {}
	 
	for _, tool in pairs(plr.Backpack:GetChildren())do
		if game.ReplicatedStorage.Weapons:FindFirstChild(tool.Name)then
			table.insert(toolsTable, tool.Name)
		end
	end
	
	local success, errorMessage = pcall(function()
		dataStore:SetAsync(plr.UserId .. "tools", toolsTable)
	end)
	
end)
 game:BindToClose(function()
	for _, plr in pairs(game.Players:GetPlayers())do
		local toolsTable = {}
	 
	for _, tool in pairs(plr.Backpack:GetChildren())do
		if game.ReplicatedStorage.Weapons:FindFirstChild(tool.Name)then
				table.insert(toolsTable, tool.Name)
				
		end
		end
		
	
	local success, errorMessage = pcall(function()
		dataStore:SetAsync(plr.UserId, toolsTable)
		end)
		
	end
end)


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