Problem While Saving Tool Data

Hello

I want to load the tools that the player owns. But It does not work.

I tried using print and searching, but i couldn’t find a solution.

ToolsToSave works, but PlayerTools is nil

local DS = game:GetService("DataStoreService")
local DataStore = DS:GetDataStore("DataStores")
local ServerStorage = game:GetService("ServerStorage")
local ToolFolder = ServerStorage:WaitForChild("ToolFolder")

game.Players.PlayerAdded:Connect(function(plr)
	local success,errormsg = pcall(function()
		
		plr.CharacterAdded:Wait()
		
		local PlayerTools = DataStore:GetAsync(plr.UserId.."-Tools")
		
		warn(PlayerTools)
		
		for i,v in pairs(ToolFolder:GetChildren()) do
			if table.find(PlayerTools,v.Name) then
				local Clone = v:Clone()
				Clone.Parent = plr.Backpack
			end
		end
		
	end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success,errormsg = pcall(function()
		
		if not plr.Character then return end
		
		if not plr.Character:FindFirstChild("Humanoid") then return end
		
		local hum = plr.Character:FindFirstChild("Humanoid")
		
		local ToolsToSave = {}
		
		hum:UnequipTools()
		
		for i,v in pairs(plr.Backpack:GetChildren()) do
			if v:IsA("Tool") then
				table.insert(ToolsToSave,v.Name)
			end
		end
		
		warn(ToolsToSave)
		DataStore:SetAsync(plr.UserId.."-Tools",ToolsToSave)
	end)
end)
2 Likes

When using GetAsync on a key that’s being used for the very first time, or that key doesn’t contain any data, it will return nil even if successfully called

If you’ve used `plr.UserId…“-Tools” before but it’s still returning nil then there are 2 possible causes (in your case I think both are happening):

  • You aren’t creating a player tools table for the player when they first join the game, you’re only creating it when saving
  • You’ll need to add this at the bottom of your script:
if game:GetService("RunService"):IsStudio() then
	game:BindToClose(function()
		task.wait(1)
	end)
end
1 Like

i tried these but it did not help :pensive:

Actually you do have to do them in order for the data store to work correctly

I’ll try to write an example data store for you and will update this comment when I’m done

Example DataStore:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local RunService = game:GetService("RunService")

local DataStore = DataStoreService:GetDataStore("DataStores")

local ToolFolder = ServerStorage.ToolFolder

local function onPlayerAdded(player)
	local success, playerTools = xpcall(DataStore.GetAsync, warn, DataStore, player.UserId.."-Tools")

	if success and playerTools then
		for _, tool in ToolFolder:GetChildren() do
			if playerTools[tool.Name] then
				local clone = tool:Clone()
				clone.Parent = player.Backpack
			end
		end
	end
end

local function onPlayerRemoving(player)
	local playerTools = {}

	for _, tool in player.Backpack:GetChildren() do
		playerTools[tool.Name] = true
	end

	xpcall(DataStore.SetAsync, warn, DataStore, player.UserId.."-Tools", playerTools)
end

if RunService:IsStudio() then
	game:BindToClose(function()
		task.wait(1)
	end)
else
	game:BindToClose(function()
		local x, y = 0, 0

		local spawn = task.spawn
		local wait = task.wait

		local function onBindToClose(player)
			onPlayerRemoving(player)

			y += 1
		end

		for _, player in Players:GetPlayers() do
			x += 1

			spawn(onBindToClose, player)
		end

		repeat wait(0) until y == x
	end)
end

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)

It worked! Thank you. I’m not really good with data stores

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.