Having trouble saving a table in datastore2

  1. What do you want to achieve? Keep it simple and clear!
    I want to save a table. The saving works… sometimes. Othertimes people randomly lose their data. I know its not a problem with servers because their int values save correctly.

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I originally used this How to save Dictionary using DataStore2? - #13 by ForeverHD? to help make it, but I can’t see how to go from here.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
I’m gonna include the important parts of the code.

local dataStore2 = require(1936396537)
dataStore2.Combine("MasterKey", "StageStore", "DeathStore", "CoinsStore", "DiamondsStore", "SwordSave")


local function setUpSwords()
	OwnedSwords = {
		"ClassicSword"
	}
	return OwnedSwords
end

local function onPlayerAdded(player)
	if player then
		print("player.Name")
		
		local stageStore = dataStore2("StageStore", player)
		local deathStore = dataStore2("DeathStore", player)
		local coinsStore = dataStore2("CoinsStore", player)
		local diamondsStore = dataStore2("DiamondsStore", player)
		local OwnedSwords = dataStore2("SwordSave", player):Get(setUpSwords())
		
		if OwnedSwords and setUpSwords() then
			wait(.5)
			saves:FireClient(player, OwnedSwords)

		for i, v in pairs(OwnedSwords)do 
--sometimes errors here
			print("Loaded: ".. v)
		end
	end
end

players.PlayerAdded:Connect(onPlayerAdded)

saves.OnServerEvent:Connect(function(player, sword)

	local savedSwords = dataStore2("SwordSave", player)
	table.insert(OwnedSwords, sword)

	dataStore2("SwordSave", player):Set(OwnedSwords)

	-- i personally think this is where the problem is, but I'm not sure
	
end)

No errors, when it saves everything seems to go fine. Although I did have an error I saw in the dev console for one player who joined that said something like “table expected got string instead”. This happened 4 days ago and I had been on vacation so I wasn’t able to get a screenshot of the error so sorry for that. I have not personally been able to replicate it so any help would be appreciated. Thanks!

I’ve just checked your code, and I’ve seen many and many mistakes you’ve done.

  1. You’re using a remote event that is being called from the client to save the data. This is a bad practice because an exploiter might constantly fire this remote event, and so it will lag your game and break your script aswell,
  2. You have no variable called OwnedSwords in your script.

table.insert(OwnedSwords, sword)

  1. You’re once again initializing the constructor, even though you declared it in a variable

local savedSwords = dataStore2(“SwordSave”, player)
dataStore2(“SwordSave”, player):Set(OwnedSwords)

I think the problem is that you have no variable called “OwnedSwords” in your code. That’s why it doesn’t work at all.

He does have a variable called OwnedSwords??

It’s not global, it’s been only locally declared in a function.

Ah yes I see now, that is probably the issue, however how would that possibly be the main one when “sometimes” it works

1 Like

I don’t know what makes it work “sometimes” but, the problem is simple, OwnedSwords has not been declared, and I am still wondering why roblox studio doesn’t consider that as an error.

I think it gives a warning of an “unknown global” I think

Um I’m not sure how that local got there I’ll edit it out, but normally owned swords is part of the function, and I have it print every time, and the printing works.
1 I did not know that thanks for explaining… what would you suggest I do instead? I guess I should do it in the remote funcition where they buy the sword.

3 Yeah that’s cause I originally tried
dataStore2(“SwordSave”, player):Set(savedSwords)
cause that’s what the tutorial said to do, but that didn’t work it basically infinitely tried setting the data and crashed the game.

Like I said it works for some people, but others they lose their data. For me i haven’t lost my data, but I know other people have.

1 Like