Table not saving and loading

This script I made is meant to save this table, and load the players purchased characters, but it dosent work.

game.Players.PlayerAdded:Connect(function(plr)
	local Characters = {}
	local gold = Instance.new("NumberValue",plr)
	gold.Name = "Gold"


	local datastore = game:GetService("DataStoreService")
	local data = datastore:GetDataStore('DataStore'..plr.UserId)

	local chars = Instance.new('Folder',plr)
	chars.Name = "Characters"
	
	
	
	
	gold.Value = data:GetAsync(plr.UserId) or 0
	
	for i,v in pairs(game.Lighting.Characters:GetChildren()) do
		
		local charfig = Instance.new("BoolValue", chars)
		charfig.Name = v.Name
		
		charfig.Changed:Connect(function()
			game.Lighting.RemoteEvents.Data.Save:Fire(plr)

			

		end)
	end
	wait(3)
	
	local character = data:GetAsync("CharacterData")

	game.Lighting.RemoteEvents.Data.Save.Event:Connect(function(plr)



		local c = plr.Characters:GetChildren()
		for i=1,#c do
			table.insert(Characters,c[i].Name.."_"..tostring(c[i].Value))
		end
		
		print(Characters)
		
		data:SetAsync("CharacterData",Characters)

		print("Saved "..plr.Name.."'s progress")
	end)

	
	
	local loaded = Instance.new('BoolValue',plr)
	loaded.Name = "DataLoaded"
	
	
	local loaded = Instance.new('ObjectValue',plr)
	loaded.Name = "SelectedMonster"
	
	
	
	gold.Changed:Connect(function()
		data:SetAsync(plr.UserId, gold.Value)
	end)
	


	game.Players.PlayerRemoving:Connect(function(plr)
		game.Lighting.RemoteEvents.Data.Save:Fire(plr)

end)	
end)




If you have any suggestions, please send them. Thanks!

1 Like

first things first, you’re not making this table unique, it needs to have a userid together with it.

SECONDLY, you’re not… loading the data.

Im sorry, but how would i load the data, im a begginer.

You have to have a for loop for the character’s table, which creates a value inside the character folder you created.

Could you please give me an example of this sort of thing? I am having trouble.

You can search up for loops in YouTube, I don’t know really how to explain them.

a loop that would run for everything in a table would look something like

for i,v in pairs(<Table Goes Here>) do
      -- Your code
end

i = Loop Number
v = Current Object

So if my table was like

local Table = {
 Value1 = "WorldId: 82452363",
 Value2 = "PlayerLevel: 19",
 Value3 = "PlayerMaxhealth: 200",
 Value4 = "PlayerHealth = 93"
}

on the 2nd loop v = Value2 and i = 2

and i and v dont have to be i and v they could be o and p

also I would recommend putting

game:BindToClose(function()
     for i,v in pairs(game.Players:GetPlayers()) do
          if v then -- Making sure the player is still in the game
              v:Kick("Server Shutting Down Your Data Should Save") -- Kicking the player so the scripts/lines of code to save data run
          end
     end
     wait(5) -- Making sure the scripts/lines of code to save data run completely and data is saved to the DataStore
end

at the end of your script so that way your save data lines can run safely before the server shuts down
and this will make it seem like your studio crashed when you click stop after doing a play test but it doesn’t