I have holding a item won't save when I leave data store issue (SOLVED)

Hello guys, I have an issue with my item data store where if you hold an item and then leave with it, it does not save. But if you don’t hold an item, It will save.

Here’s the code:

local dss = game:GetService("DataStoreService") 
local toolds = dss:GetDataStore("ToolDS") 

local toolcontain = game.ServerStorage.Items 



game.Players.PlayerRemoving:Connect(function(player)
	wait(2)
	local playerbp = {} 
	for i, v in ipairs(player.Backpack:GetChildren()) do 
		if v:IsA('Tool') then
			table.insert(playerbp, v.Name) 
		end
		
	end
	
	toolds:SetAsync(player.UserId, playerbp)
end)

game.Players.PlayerAdded:Connect(function(player)
	local playerbp = toolds:GetAsync(player.UserId) or {} 
	for i, v in ipairs(playerbp) do
		if toolcontain:FindFirstChild(v) then 
			toolcontain[v]:Clone().Parent = player.Backpack
			toolcontain[v]:Clone().Parent = player.StarterGear 
		end
	end
end)

game:BindToClose(function()
	wait(3)
end)
3 Likes

Make sure to save the tool inside the character model too, when you equip a tool, it goes from your backpack to your character model

I have tried that but if gives this error
ServerScriptService.ItemDataStore:18: attempt to index nil with ‘GetChildren’
Here is the code:

for i, v in ipairs(player.Character:GetChildren()) do 
		if v:IsA('Tool') then
			table.insert(playerbp, v.Name) 
		end

	end

hmmm, try putting that in CharacterRemoving event

I don’t understand how to do that. Can you give an example?

ummm, lemme make the code for a moment

1 Like
local plrCharacterTools = {}

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterRemoving:Connect(function(char)
		plrCharacterTools[plr.UserId] = char:FindFirstChildOfClass("Tool")
	end)

	local playerbp = toolds:GetAsync(player.UserId) or {} 
	for i, v in ipairs(playerbp) do
		if toolcontain:FindFirstChild(v) then 
			toolcontain[v]:Clone().Parent = player.Backpack
			toolcontain[v]:Clone().Parent = player.StarterGear 
		end
	end
end)

and then in your ‘PlayerRemoving’ you can access the plrCharacterTools dictionary

I tried it, it does not work.

.

Does the script also gives an error in output?

Have you tried this without being in Roblox studio? Roblox Studio doesn’t handle BindToClose very well. You can work around this if this is the case. Make sure you are in Roblox Studio and then do a time loop to save

Did you get an error or something?

I didn’t say an error in the console

local dss = game:GetService("DataStoreService") 
local toolds = dss:GetDataStore("ToolDS") 

local toolcontain = game.ServerStorage.Items 



game.Players.PlayerRemoving:Connect(function(player)
	wait(2)
	local playerbp = {} 
	for i, v in ipairs(player.Backpack:GetChildren()) do 
		if v:IsA('Tool') then
			table.insert(playerbp, v.Name) 
		end
	end
	
	if player.Character:FindFirstChildWhichIsA("Tool") then
		table.insert(playerbp,player.Character:FindFirstChildWhichIsA("Tool").Name)
	end

	toolds:SetAsync(player.UserId, playerbp)
end)

game.Players.PlayerAdded:Connect(function(player)
	local playerbp = toolds:GetAsync(player.UserId) or {} 
	for i, v in ipairs(playerbp) do
		if toolcontain:FindFirstChild(v) then 
			toolcontain[v]:Clone().Parent = player.Backpack
			toolcontain[v]:Clone().Parent = player.StarterGear 
		end
	end
end)

game:BindToClose(function()
	wait(3)
end)

This should work

can you put

game:BindToClose(function()
	wait(1)
end)

It returns an error saying ServerScriptService.ItemDataStore:24: attempt to index nil with ‘FindFirstChildWhichIsA’

Hey I just found out that CharacterRemoving fires after PlayerRemoving, to counter this, we can write code like this:

local Players = game:GetService("Players")

local playerRemovingBPDatas = {}

Players.PlayerAdded:Connect(function(plr)
	plr.CharacterRemoving:Connect(function(char)
		print("char removing")
		
		if playerRemovingBPDatas[tostring(plr.UserId)] then
			local tool = char:FindFirstChildOfClass("Tool")
			if tool then
				table.insert(playerRemovingBPDatas[tostring(plr.UserId)], tool.Name)
			end
			
			print(playerRemovingBPDatas[tostring(plr.UserId)]) -- print all tools for proof
			-- DO YOUR DATA SAVING HERE

			-- after saving data, you can also do "playerRemovingBPDatas[tostring(plr.UserId)] = nil" to clear that user backpack data in the dictionary to save game memory

		end
	end)
end)

Players.PlayerRemoving:Connect(function(plr)
	local plrBP = {}
	for _,tool in pairs(plr.Backpack:GetChildren()) do
		if tool:IsA("Tool") then
			table.insert(plrBP, tool.Name)
		end
	end
	playerRemovingBPDatas[tostring(plr.UserId)] = plrBP
end)

Please tell me if it works

The reason it didn’t get the character is because you had an unnecessary wait

local dss = game:GetService("DataStoreService") 
local toolds = dss:GetDataStore("ToolDS") 

local toolcontain = game.ServerStorage.Items 



game.Players.PlayerRemoving:Connect(function(player)
	local playerbp = {} 
	for i, v in ipairs(player.Backpack:GetChildren()) do 
		if v:IsA('Tool') then
			table.insert(playerbp, v.Name) 
		end
	end
	
	if player.Character:FindFirstChildWhichIsA("Tool") then
		table.insert(playerbp,player.Character:FindFirstChildWhichIsA("Tool").Name)
	end

	toolds:SetAsync(player.UserId, playerbp)
end)

game.Players.PlayerAdded:Connect(function(player)
	local playerbp = toolds:GetAsync(player.UserId) or {} 
	for i, v in ipairs(playerbp) do
		if toolcontain:FindFirstChild(v) then 
			toolcontain[v]:Clone().Parent = player.Backpack
			toolcontain[v]:Clone().Parent = player.StarterGear 
		end
	end
end)

game:BindToClose(function()
	task.wait(3)
end)

Thanks it worked! And thanks to everyone who helped.

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