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

Cannot store Instance in data store. Data stores can only accept valid UTF-8 characters??

I don't understand how to store the instances? I'm trying this but it won't work. Any fixes?

Serverscript

game.Players.PlayerRemoving:Connect(function(player)
	
	warn("Data Saving")
	
	local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."tHats01")
	
	local statstorage = player:WaitForChild("Inventory").Hats:GetChildren()
	
	for i = 1, #statstorage do
		
		datastore:SetAsync(statstorage[i].Name, statstorage[i])
		
		print("Saved Data Number ("..i..")")
	end
	print("Stats Successfully Saved!")
end)

game.Players.PlayerAdded:Connect(function(player)
	
	warn("Data Loading")
	
	local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."tHats01")
	local MainCurrency = player:WaitForChild("MainCurrency")
	
	if MainCurrency then
	wait(1)
	local stats = player:WaitForChild("Inventory").Hats:GetChildren()
	
	for i = 1, #stats do
		
		stats[i].Value = datastore:GetAsync(stats[i].Name)
		print("stats number ("..i..") has been found")
		
		end
	end
end)

1 comment

0

which line is having the error?

3 answers

you would have to store the name of instances in datastore, then whenever u want to reference an instance, you simply pick it out using it's name.. its not that complicating trust me..

simply just put the name of the instances in a table, then store the table in datastore

1 comment

0

I don't quite understand

Try using datastore:SetAsync(statstorage[i].Name, game:GetService("HttpService"):JSONEncode()) -- table with all the names in here , then load in using local stats = game:GetService("HttpService"):JSONDecode() -- table from datastore here

2 comments

0

Idk if it will work...

0

then,pick it out using its name, like the guy above said

I tried to store the folder, now I'm getting the error "ServerScriptService.Datastore.Hats:9: attempt to get length of a userdata value"

Serverscript

game.Players.PlayerRemoving:Connect(function(player)
	
	warn("Data Saving")
	
	local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."tHats01")
	
	local statstorage = player:WaitForChild("Inventory")
	
	for i = 1, #statstorage do
		
		datastore:SetAsync(statstorage[i].Name, statstorage[i])
		
		print("Saved Data Number ("..i..")")
	end
	print("Stats Successfully Saved!")
end)

game.Players.PlayerAdded:Connect(function(player)
	
	warn("Data Loading")
	
	local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."tHats01")
	local MainCurrency = player:WaitForChild("MainCurrency")
	
	if MainCurrency then
	wait(1)
	
	local stats = player:WaitForChild("Inventory")
	
	for i = 1, #stats do
		
		stats[i].Value = datastore:GetAsync(stats[i].Name)
		print("stats number ("..i..") has been found")
		
		end
	end
end)

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