Storing multiple pieces of data error and optimization help

I’m working on a feature that stores what kind of goblins you killed already. The problem is there’s going to be multiple goblins added in each update and I thought that adding a list would make it easier to add new goblins but I’ve stumbled upon many problems like the value saves as null, unable to cast value to obj, ect. All these problems seemed to occur when I added this and affected all the other saved values. I also would like to know if there’s a way store this many pieces of data in a better way.

local DS = game:GetService("DataStoreService"):GetDataStore("SaveMyData")
local GB = game:GetService("DataStoreService"):GetDataStore("AllGoblins")
local http = game:GetService("HttpService")

local goblinbook = {

	Goblin = 0,
	Goblin2 = 0,
	Goblin3 = 0,
	Goglin = 0,
	Godlin = 0,
	FatGoblin = 0,
	ArmoredGob = 0,
	ToyArmyGob = 0,
	Lightlin = 0,
	Darklin = 0,
	GodlinJr = 0,
	ArmyGob = 0,
	ArmyGob2 = 0,
	ArmyGob3 = 0,

}



game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
------------------------------------------------------------
	local Currency = Instance.new("IntValue",leaderstats)
	Currency.Name = "Springs"
	Currency.Value = 0
	
	local Currency = Instance.new("IntValue",leaderstats)
	Currency.Name = "Streak"
	Currency.Value = 0 
-------------------------------------------------------------	
	local setting = Instance.new("Folder", player)
	setting.Name = "Setting"
	
	local Shadows = Instance.new("BoolValue", setting)
	Shadows.Name = "Shadows"
	Shadows.Value = true
	
	local RagdollFix = Instance.new("BoolValue", setting)
	RagdollFix.Name = "RagdollFix"
	RagdollFix.Value = false
	
	local Music = Instance.new("BoolValue", setting)
	Music.Name = "Music"
	Music.Value = true
--------------------------------------------------------------
	local plrkey = "id_"..player.userId
	local savevalue1 = player.leaderstats.Springs
	local savevalue2 = player.leaderstats.Streak
	local savevalue3 = player.Setting.Shadows
	local savevalue4 = player.Setting.RagdollFix
	local savevalue5 = player.Setting.Music

	local GetSaved = DS:GetAsync(plrkey)
	if GetSaved then
		print(GetSaved)
		savevalue1.Value = GetSaved[1]
		savevalue3.Value = GetSaved[2]
		savevalue4.Value = GetSaved[3]
		savevalue5.Value = GetSaved[4]

	else
		local NumbersForSaving = {savevalue1, savevalue3, savevalue4, savevalue5}
		DS:GetAsync(plrkey, NumbersForSaving)
	end

	--------------------------------------------------------------
	local gobbook = Instance.new("Folder", player)
	gobbook.Name = "GoblinNotes"

	local numb = 1
	local NumbersForSavingg = {}
	for name, value in pairs(goblinbook) do

		local thing = Instance.new("IntValue", gobbook)
		thing.Name = name
		thing.Value = 0
		local plrkey = "id_"..player.userId
		local GetSaved = GB:GetAsync(plrkey)
		if GetSaved and GetSaved[numb] ~= nil then	
			thing.Value = GetSaved[numb]
			numb += 1
		else
			
			for _, x in pairs(player.GoblinNotes:GetChildren()) do
				print(x)
				table.insert(NumbersForSavingg, x)
			end
			
		end
	end
	print(NumbersForSavingg)
	--DS:GetAsync(plrkey, NumbersForSaving)
	GB:GetAsync(plrkey, NumbersForSavingg)
	--------------------------------------------------------------
end)

game.Players.PlayerRemoving:Connect(function(player)
	local stuff = {}
	for _, x in player.GoblinNotes:GetChildren() do
		table.insert(stuff, x.Value)
	end
	print(stuff)
	DS:SetAsync("id_"..player.userId, {player.leaderstats.Springs.Value, player.Setting.Shadows.Value,player.Setting.RagdollFix.Value,player.Setting.Music.Value})
	GB:SetAsync("id_"..player.userId, http:JSONEncode(stuff))
end)
2 Likes

I suggest saving them as a dictionary with either a true or false value representing whether or not they have been killed, I also strongly recommend using profile service module as it is much easier and has good functionalities

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.