DataStore Instance Name won't work

Hello, this is a DataStore that I’ve made that checks for what items the player has in their inventory, saves the Names in a table and then next time they come back those items are sent from the ReplicatedStorage to their inventory.

dss = game:GetService('DataStoreService')
contributorDS = dss:GetDataStore('contributorDS')
player = game:GetService('Players')
rs = game:GetService('ReplicatedStorage')

game.Players.PlayerAdded:Connect(function(player)
	print("Player "..player.Name.." added")
	local playerUserId = player.UserId

	local data
	local success, errorMessage = pcall(function()
		data = contributorDS:GetAsync("UserInventory-"..playerUserId) --Load
	end)

	if success then
		task.wait()
		print(player.Name.."'s Accessory Inventory Loaded Succesfully!")

		local folder = rs:FindFirstChild('Sample'):Clone()
		local isSample = rs:FindFirstChild('ItemStorage')
		folder.Name = 'Folder_'..playerUserId
		folder.Parent = rs

		if folder.Name == 'Folder_'..playerUserId then
			
			
			for i ,acc in pairs(isSample:GetChildren()) do
				print(acc.Name)
				print(data)
				
				--// Not matching data names
				if acc.Name == data then
					print(acc.Name..' found')
				end
			end

		else
--[[
			for _,acc in pairs(isSample:GetChildren()) do
				acc.Parent = folder

			end

			for _, child in pairs(folder:GetChildren()) do
				child.Parent = player.Inventory

			end

			print(folder:GetChildren())
						]]

			return
		end


	else
		warn("Error: "..errorMessage)

	end	

end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = player.UserId

	if player:FindFirstChild("Inventory") then
		local accessorySave = {}

		for _,acc in pairs(player.Inventory:GetChildren()) do
			print("   Obj: "..acc.Name)
			table.insert(accessorySave,acc.Name)
		end

		local success, errorMessage = pcall(function()
			contributorDS:SetAsync("UserInventory-"..player.UserId, accessorySave) --Save
		end)

		if success then
			print(player.Name.."'s Accessory Inventory Saved Succesfully!")
		else
			warn("Error: "..errorMessage)
		end
	end
end)

however idk why this part doesn’t work

 for i ,acc in pairs(isSample:GetChildren()) do
				print(acc.Name)
				print(data)
				
				--// Not matching data names
				if acc.Name == data then
					print(acc.Name..' found')
				end
			end

sorry if I don’t reply I really need some sleep I’ve been coding from 12 PM to 6AM so ill probably reply whenever I’m awake

still don’t know how to fix this sadly…

Hi! Sorry that you never got a response.

Can you explain how it isnt working? Anything in output?

1 Like

ah, well yeah it does print the saved inventory file names but, i can’t seem to be able to use them sadly…

17:36:17.259 Player ShinonArtZ added - Server - InventoryData:7
17:36:17.633 ShinonArtZ’s Accessory Inventory Loaded Succesfully! - Server - InventoryData:17
17:36:17.633 ▼ {
[1] = “unusual_selfmade”,
[2] = “compass”,
[3] = “Lamp”
} - Server - InventoryData:26
17:36:17.633 Lamp - Server - InventoryData:28
17:36:17.633 compass - Server - InventoryData:28
17:36:17.633 unusual_selfmade - Server - InventoryData:28

it doesn’t seem to be able to use the name or i guess find it?, its supposed to do

if acc.Name == data then
	print(acc.Name..' found')
end

once it finds them

Can you tell me what the print(acc.Name) and the print(data) lines output?

1 Like

sorry for slow reply, i was able to get this working, however i still have issues sometimes where even tho the player had items sometimes it just doesnt register

dss = game:GetService('DataStoreService')
contributorDS = dss:GetDataStore('contributorDS')
player = game:GetService('Players')
rs = game:GetService('ReplicatedStorage')

game.Players.PlayerAdded:Connect(function(player)
	--print("Player "..player.Name.." added")
	local playerUserId = player.UserId
	local inv = player:FindFirstChild('Inventory')
	
	local data
	local success, errorMessage = pcall(function()
		data = contributorDS:GetAsync("UserInventory-"..playerUserId) --Load
	end)

	if success then
		--print(player.Name.."'s Accessory Inventory Loaded Succesfully!")

		local folder = rs:FindFirstChild('Sample'):Clone()
		local isSample = rs:FindFirstChild('ItemStorage')
		folder.Name = 'Folder_'..playerUserId
		folder.Parent = rs

		if folder.Name == 'Folder_'..playerUserId then
			
			--print(data)
			for i,v in pairs(data) do
				rs:FindFirstChild('ItemStorage')[v]:Clone().Parent = folder
			end
			for i, child in pairs(folder:GetChildren()) do
				child.Parent = inv
			end
			--print(inv:GetChildren())
			
		else
			
		end

	else
		warn("Error: "..errorMessage)

	end	
	

end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = player.UserId

	if player:FindFirstChild("Inventory") then
		local accessorySave = {}

		for _,acc in pairs(player.Inventory:GetChildren()) do
			--print("   Obj: "..acc.Name)
			table.insert(accessorySave,acc.Name)
		end

		local success, errorMessage = pcall(function()
			contributorDS:SetAsync("UserInventory-"..player.UserId, accessorySave) --Save
		end)

		if success then
		--	print(player.Name.."'s Accessory Inventory Saved Succesfully!")
		else
			warn("Error: "..errorMessage)
		end
	end
end)

basically same code except now i have this

for i,v in pairs(data) do
rs:FindFirstChild('ItemStorage')[v]:Clone().Parent = folder
end
for i, child in pairs(folder:GetChildren()) do
child.Parent = inv
end

Hmm. Try printing the returned contents of the datastore to check if anything is different when it doesn’t register.

1 Like

sure thing, here:

18:50:03.928 {} - Server - InventoryData:40

its just empty, when i do have items still it shows this instead:

18:50:30.798 ▼ {
[1] = “unusual_selfmade”,
[2] = “compass”,
[3] = “Lamp”
} - Server

it saves the items sometimes and sometimes not, its really odd and not consistent for some reason :confused:

May be because the datastore failed for some reason. Try looping your pcall a few times if success is nil, and check if that improves the success rate.

1 Like

seems to be that this part

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = player.UserId
	local accessorySave = {}
	local inv = player:FindFirstChild('Inventory')

	if inv then

		for _,acc in pairs(inv:GetChildren()) do
			--print("   Obj: "..acc.Name)
			table.insert(accessorySave,acc.Name)
			
		end

		local success, errorMessage = pcall(function()
			contributorDS:SetAsync("UserInventory-"..player.UserId, accessorySave) --Save
		end)

		if success then
			--	print(player.Name.."'s Accessory Inventory Saved Succesfully!")
			print(accessorySave)
			
		else
			warn("Error: "..errorMessage)
			
		end
	end
end)

is not saving in time

18:58:57.754 Disconnect from ::ffff:127.0.0.1|63109 - Studio
18:58:57.896 {} - Server

playerremoving isnt catching the items in time

Any warning in output?

hererobloxhaveyour30

1 Like

no warnings at all, it just isnt catching them in time is all, idk why exactly :/, ill mess with the code a bit

Odd, not sure why that happens. Let me know if figure out anything.

1 Like

I always find issues with PlayerRemoving in studio. Maybe try this in a public game server?

(I know you wont see output but you can see the result if your tools saved)

1 Like

good idea, ill try that, i still need to code when the character respawns to give their respective items back but thats after this main part is done

edit: didn’t save no sadly, so yeah its an issue with playerremoving that just doesnt manage to save the data in time

The loop and the pcall could be the culprit, however I am unsure. Try to optimise your code in any way you can that does not affect your script.

EDIT: Make sure accessorysave isnt nil. Print the table at the end of the loop and see if it returns empty or full.

EDIT 2: If it returns empty, print inv:GetChildren(). Does that return nil? Does the if inv then even pass?

1 Like

yeah i’ve got no clue what the issue is and it works worse now for some reason, this is my first time using DataStore so i have no clue what im exactly doing im kind of just moving stuff around until it works

ServerScriptService.InventoryData:28: attempt to index nil with ‘GetChildren’

yes the value is sometimes returning nil for when the folders dont get the instances

alright guys, thanks everybody for the help, this code works ( currently, if i see any issues ill make sure to update the code )

dss = game:GetService('DataStoreService')
contributorDS = dss:GetDataStore('contributorDS')
player = game:GetService('Players')
rs = game:GetService('ReplicatedStorage')

game.Players.PlayerAdded:Connect(function(player)
	local playerUserId = player.UserId
	
	local data
	local success, errorMessage = pcall(function()
		data = contributorDS:GetAsync("UserInventory-"..playerUserId) --Load
	end)

	if success then
		local folder = rs:FindFirstChild('Sample'):Clone()
		local isSample = rs:FindFirstChild('ItemStorage')
		folder.Name = 'Folder_'..playerUserId
		folder.Parent = rs
		
		if folder.Name == 'Folder_'..playerUserId then
			print(data)

			for i,v in pairs(data) do
				isSample[v]:Clone().Parent = folder
				print(folder:GetChildren())
			end
			
			for i, child in pairs(folder:GetChildren()) do
				child.Parent = player:WaitForChild('Inventory')
				print(player.Inventory:GetChildren())
			end
		else
			print('Player_'..playerUserId..' had no items')
		end

	else
		warn("Error: "..errorMessage)

	end	


end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = player.UserId
	local accessorySave = {}
	local inv = player:FindFirstChild('Inventory')

	for i,acc in pairs(inv:GetChildren()) do
		table.insert(accessorySave, acc.Name)
	end

	local success, errorMessage = pcall(function()
		contributorDS:SetAsync("UserInventory-"..player.UserId, accessorySave) --Save
		print(accessorySave)
	end)

end)
1 Like