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

Why is my script expecting a string for a table datastore?

Hello.

I'm kind of new to datastores, I can save a single value, however more than that is where the issues begin.

So, basically, I'm trying to add multiple integer values into a folder called "inventory", then when the player leaves, the data saves, and when they rejoin, the values load back into the user's folder, named "inventory".

Here's my script.

local DS = game:GetService("DataStoreService"):GetDataStore("Inventories")

game.Players.PlayerAdded:Connect(function(player)

	local folder = Instance.new("Folder",player)
	folder.Name = "inventory"
	
	local inventory = {}
	inventory.Name = "inventory"



	local dataKey = "userkey_"..player.UserId
	
	local data
	
	local success,errormsg = pcall(function()
		data = DS:GetAsync(dataKey)
	end)
	

	if not success then
		warn("Error when retrieving data for "..player.Name..". Error: "..errormsg)
		folder.Value = nil
		
	end
	
	end)

game.Players.PlayerRemoving:Connect(function(player)
	local success,errormsg = pcall(function()
		for i, v in pairs(game.Players[player].inventory:GetFirstChildOfClass("IntValue").Value) do
			DS:SetAsync("userkey_"..player.UserId,player.inventory.v.Value)
		end
	end)
	if not success then
		warn("Unable to save for "..player.Name..". Error: "..errormsg)
	end
end)

The error:

Unable to save for 7z99. Error: ServerScriptService.Saving game data:32: bad argument #2 (string expected, got Object)

Thanks in advance for the help!

1 answer

Try using this script

local ds2 = require(1936396537)
local plrs = {}
function playeradded(player)
	local Inventorysave = ds2("Inventory", player)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player 
	
	local inventory = Instance.new("IntValue")
	inventory.Name = "Inventory"

end
?
function save(player)
	local Inventorysave = ds2("Inventory", player)

	Inventorysave(player.leaderstats.Inventory.Value)?
end
game.Players.PlayerRemoving:Connect(function(player)
	save(player)
end)
game.Players.PlayerAdded:Connect(function(player)
	table.insert(plrs, #plrs + 1, player.Name)
	playeradded(player)
end)
?
function check(tbl, thing)
	for i, v in pairs(tbl) do
		if v == thing then
			return true
		end
	end
end
?
for i, v in pairs(game.Players:GetPlayers()) do
	if not check(plrs, v.Name) then
		table.insert(plrs, #plrs + 1, v.Name)
		playeradded(v)
	end
end

ds2 Should work way better try it I might have made mistakes but try it

7 comments

0

Thanks! Could you add some comments to explain what's going on? This stuff is pretty new to me.

0

There ended up being an issue: "Inventory is not a valid member of Folder", and then at the end, it says the inventory was not saved because it wasn't updated.

0

Oh gosh sorry about that I made some mistakes

0

Lemme update it

0

There that should work and also delete the ? idk why it shows that

0

It appears to be the same.

0

There was " but sideways

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