Nil value error in my datastore2 script

so i tried to make a datastore script, it worked fine with the stats , but when i tried to add souls it’s gave me an error, i tried to fix it by finding difference between the achivments and my soul thing, i found nothing different…

basically , there’s an nil value error in those lines


which are , BasicSoul.Value = SoulsData:Get(UpdateValue).BasicSoul
and UpdateSouls(UserData.SOULS)

--variables
local DataStore2 = require(game:GetService("ServerScriptService").MainModule)
local mainkey = "Gaster"
DataStore2.Combine(mainkey, "Stats", "Achivments","SOULS","ITEMS","FOOD","ARMOR")

-- data table
local function SetDataTable()
	local UserData = {
		Stats = {
			["EXP"] = 0,
			["GOLD"] = 0,
			["RESET"] = 0,
			["LOVE"] = 1
			},
		Achivments = {
			["KilledSans"] = false,
			["GotHateSoul"] = false,
		},
		SOULS = {
			["BasicSoul"] = true,
			["Determination"] = false,
			["Integrity"] = false,
			["Justice"] = false,
			["Perseverance"] = false,
			["Kindness"] = false,
			["Bravery"] = false,
			["Patience"] = false
		},
		ITEMS = {
			["Stick"] = true,
			["REALKnife"] = false	
		},
		ARMOR = {
			["Bandage"] = true,
			["HeartLocket"] = false	
		},
		FOOD = {
			["MonsterCandy"] = 0,	
		},
	}
	return UserData
end

--main

game.Players.PlayerAdded:Connect(function(plr)
	local UserData = DataStore2(mainkey,plr):Get(SetDataTable())
	
	local gaster = Instance.new("Folder")
	gaster.Name = "TobyFox"
	local soulsfolder = Instance.new("Folder")
	soulsfolder.Name = "SOULS"
	local ITEMS = Instance.new("Folder")
	ITEMS.Name = "ITEMS"
	local ARMOR = Instance.new("Folder")
	ARMOR.Name = "ARMOR"
	local FOOD = Instance.new("Folder")
	FOOD.Name = "FOOD"
	local achfolder = Instance.new("Folder")
	achfolder.Name = "AchivmentFolder"
	
	--userStats
	local LOVE = Instance.new("IntValue")
	LOVE.Name = "LOVE"
	local GOLD = Instance.new("IntValue")
	GOLD.Name = "GOLD"
	local RESET = Instance.new("IntValue")
	RESET.Name = "RESET"
	local EXP = Instance.new("IntValue")
	EXP.Name = "EXP"
	
	--souls
	local BasicSoul = Instance.new("BoolValue")
	BasicSoul.Name = "BasicSoul"
	local Determination = Instance.new("BoolValue")
	Determination.Name = "Determination"
	local Justice = Instance.new("BoolValue")
	Justice.Name = "Justice"
	local Integrity = Instance.new("BoolValue")
	Integrity.Name = "Integrity"
	local Bravery = Instance.new("BoolValue")
	Bravery.Name = "Bravery"
	local Kindness = Instance.new("BoolValue")
	Kindness.Name = "Kindness"
	local Patience = Instance.new("BoolValue")
	Patience.Name = "Patience"
	local Perseverance = Instance.new("BoolValue")
	Perseverance.Name = "Perseverance"
	
	--items
	local Stick = Instance.new("BoolValue")
	Stick.Name = "Stick"
	local REALKnife = Instance.new("BoolValue")
	REALKnife.Name = "REALKnife"
	
	--Armor
	local Bandage = Instance.new("BoolValue")
	Bandage.Name = "Bandage"
	local HeartLocket = Instance.new("BoolValue")
	HeartLocket.Name = "HeartLocket"
	
	--Achivments
	local ripsans = Instance.new("BoolValue")
	ripsans.Name = "KilledSans"
	local HATE = Instance.new("BoolValue")
	HATE.Name = "GotHateSoul"
	
	--Food
	local MonsterCandy = Instance.new("IntValue")
	MonsterCandy.Name = "MonsterCandy"
	
	--StoreInFolder
	local StatsData = DataStore2("Stats", plr)
	local AchivData = DataStore2("Achivments", plr)
	local SoulsData = DataStore2("SOULS", plr)
	local ItemsData = DataStore2("ITEMS",plr)
	local FoodData = DataStore2("FOOD", plr)
	local ArmorData = DataStore2("ARMOR", plr)
	
	--Stats data
	local function UpdateStats(UpdateValue) -- updates value to current data
		LOVE.Value = StatsData:Get(UpdateValue).LOVE
		EXP.Value = StatsData:Get(UpdateValue).EXP
		GOLD.Value = StatsData:Get(UpdateValue).GOLD
		RESET.Value = StatsData:Get(UpdateValue).RESET
	end
	
	--Achivment data
	local function UpdateAch(UpdateValue)
		ripsans.Value = AchivData:Get(UpdateValue).ripsans
		HATE.Value = AchivData:Get(UpdateValue).HATE
	end
	
	--Soul data
	local function UpdateSouls(UpdateValue)
		BasicSoul.Value = SoulsData:Get(UpdateValue).BasicSoul
		Determination.Value = SoulsData:Get(UpdateValue).Determination
		Perseverance.Value = SoulsData:Get(UpdateValue).Perseverance
		Patience.Value = SoulsData:Get(UpdateValue).Patience
		Kindness.Value = SoulsData:Get(UpdateValue).Kindness
		Bravery.Value = SoulsData:Get(UpdateValue).Bravery
		Justice.Value = SoulsData:Get(UpdateValue).Justice
		Integrity.Value = SoulsData:Get(UpdateValue).Integrity
	end
	
	--update data
	UpdateStats(UserData.Stats)
	UpdateAch(UserData.Achivments)
	UpdateSouls(UserData.SOULS)
	
	StatsData:OnUpdate(UpdateStats)
	AchivData:OnUpdate(UpdateAch)
	SoulsData:OnUpdate(UpdateSouls)
	
	--parent
	gaster.Parent = plr
	achfolder.Parent = plr
	soulsfolder.Parent = plr
	
	--allinonestats
	--stats
	LOVE.Parent = gaster
	RESET.Parent = gaster
	EXP.Parent = gaster
	GOLD.Parent = gaster
	--achivments
	ripsans.Parent = achfolder
	HATE.Parent = achfolder
	--Souls
	Determination.Parent = soulsfolder
	BasicSoul.Parent = soulsfolder
	Perseverance.Parent = soulsfolder
	Integrity.Parent = soulsfolder
	Justice.Parent = soulsfolder
	Kindness.Parent = soulsfolder
	Bravery.Parent = soulsfolder
	Patience.Parent = soulsfolder

workspace.yesname.ClickDetector.MouseClick:Connect(function()
	UserData.Stats.EXP = UserData.Stats.EXP + 1
	StatsData:Set(UserData.Stats)
end)

-- LEVELSYSTEM
local function onLOVEChanged(plr, EXP, LOVE)
		local reset = false
	if UserData.Stats.LOVE >= 100 then
		UserData.Stats.LOVE = 100
		reset = true
	end
end

LOVE.Changed:connect(function()
	onLOVEChanged(plr, EXP, LOVE)
end)

EXP.Changed:connect(function()
	if UserData.Stats.LOVE < 100 then
		if UserData.Stats.EXP >=  (10 + 25*UserData.Stats.LOVE)  then
		UserData.Stats.EXP = UserData.Stats.EXP - (10 + 25*UserData.Stats.LOVE)
		UserData.Stats.LOVE = UserData.Stats.LOVE + 1
		end
	end
end)
--LEVEL SYSTEN END
end)

please help me solve this problem,thanks in advance :slight_smile:

local UserData = DataStore2(mainkey,plr):Get(SetDataTable())

On that line, you are getting their datastore. But if they don’t have an existing datastore then that value will be nil so when you try and do UserData.SOULS it errors. With information of an array not properly filled in, either with the array itself not existing or the index of an array not already being set, trying to index will return a nil value or give you an error.

To solve this you must compensate for the fact that they need to be assigned a new array if they don’t already have one. You can do this simply just by changing the line like so:

local UserData = DataStore2(mainkey,plr):Get(SetDataTable()) or {--[[array of starting values for new players]]}

Of course, you would have to organize that array so that all the data is ordered properly to prevent errors, which I will leave to you. Please let me know if this solved your issue!

1 Like

i found out that all i had to do for this to work is to change my mainkey string… lol, but thanks anyway, i appreciate it

1 Like

Okay sounds great, glad you were able to figure it out on your own!