dataStore Problem, invalid argument

Making a datastore for this game
and i came across an error that i’d never gotten before, i’ve searched already for a solution, but have not found an answer, so i come here with my hopes up
the error i receive is the following: invalid argument #3 (string expected, got nil)
the error occurs on line 155, and is referenced with a comment right after the line
code:

local store = game:GetService("DataStoreService")
local upg = store:GetDataStore('v0.001')

local villages = {
	"Leaf";
	"Mist";
	"Cloud";
	"Stone";
	"Sand";
	
}

local lastNames = {
	"Uchiha";
	"Senju";
	"Sarutobi";
	"Clanless";
	"Karatachi";
	"Hoshigaki";
	"Hozuki";
	"Clanless";
	"Uzumaki";
	"Kamizuru";
	"Aburame";
	"Clanless";
	"Houki";
	"Kazesuna";
	"Shirogane";
	"Clanless";
	"Nameless";
	"Yotsuki";
	"Nara";
	"Clanless";

}

local elements = {
	"Fire";
	"Water";
	"Earth";
	"Lightning";
	"Wind";
}



function getData(player)
	local data
	local success,err = pcall(function()
		data = upg:GetAsync(player.UserId)
	end)
	
	
	if success then
		warn(player.Name.."'s Stats have loaded")
	elseif err then
		warn(err,'error has ocurred')
	end 
	
	return data
end

function OnAdded(player)
	local folder = Instance.new("Folder", player)
	folder.Name = "Data"
	
	local thirdElementNumber, randomNumberChance = 1,math.random(1,100)
	if randomNumberChance == 1 then 
		randomNumberChance = math.random(1,50)
		if randomNumberChance == 1 then
			thirdElementNumber = 10
		end
		
	end
	
	
	
	local village = Instance.new("StringValue",folder)
	village.Name = "Village"
	village.Value = villages[math.random(1,5)]
	

	local lastName = Instance.new("StringValue",folder)
	lastName.Name = "LastName"
	lastName.Value = "LastName"
	if village.Value == "Leaf" then
		lastName.Value = lastNames[math.random(1,4)]
	elseif village.Value == "Mist" then
		lastName.Value = lastNames[math.random(5,8)]
	elseif village.Value == "Stone" then
		lastName.Value = lastNames[math.random(9,12)]
	elseif village.Value == "Sand" then
		lastName.Value = lastNames[math.random(13,16)]
	elseif village.Value == "Cloud" then
		lastName.Value = lastNames[math.random(16,19)]
	end
	
	if player.Name == "Genmatsu" then
		lastName.Value = "RinneGod"
		village.Value = "Rain"
	end
	
	local firstName = Instance.new("StringValue",folder)
	firstName.Name = "FirstName"
	firstName.Value = player.Name

	local rank = Instance.new("StringValue",folder)
	rank.Name = "Rank"
	rank.Value = "Academy Student"
	
	local kekkeiGenkai = Instance.new("StringValue",folder)
	kekkeiGenkai.Name = "KekkeiGenkai"
	kekkeiGenkai.Value = "None"
	
	local kekkeiUnlock = Instance.new("StringValue",folder)
	kekkeiUnlock.Name = "KekkeiUnlock"
	kekkeiUnlock.Value = "Type1"
	
	local kekkeiLocked = Instance.new("BoolValue",folder)
	kekkeiLocked.Name = "KekkeiIsLocked"
	kekkeiLocked.Value = true
	
	local elementOne = Instance.new("StringValue",folder)
	elementOne.Name = "ElementOne"
	elementOne.Value = elements[math.random(1,5)]
	
	local elementTwo = Instance.new("StringValue",folder)
	elementTwo.Name = "ElementTwo" 
	elementTwo.Value = elements[math.random(1,5)]
	
	local elementThree = Instance.new("StringValue",folder)
	elementThree.Name = "ElementThree" 
	elementThree.Value = "None"
	if thirdElementNumber == 10 then
		elementThree.Value = elements[math.random(1,5)]
	end
	
	
	local uiDone = Instance.new("BoolValue",folder)
	uiDone.Name = "UiDone"
	uiDone.Value = false
	
	local data = getData(player)
	
	if data then
		for i,v in pairs(data)do
			print(i,v)
		end
		--getting the values from the table
		village.Value = data.Village
		lastName.Value = data.LastName
		firstName.Value = data.FirstName
		rank.Value = data.Rank
		kekkeiGenkai.Value = data.KekkeiGenkai
		kekkeiUnlock.Value = data.KekkeiUnlock -- this is the line that is erroring
		kekkeiLocked.Value = data.KekkeiIsLocked
		elementOne.Value = data.ElementOne
		elementTwo.Value = data.ElementTwo
		elementThree.Value = data.ElementThree
		uiDone.Value = data.UiDone

	end
end

function getSaveTable(player)
	local stats = player:WaitForChild("Data")
	local village = stats.Village
	local lastName = stats.LastName
	local firstName = stats.FirstName
	local rank = stats.Rank
	local KekkeiGenkai = stats.KekkeiGenkai
	local KekkeiUnlock = stats.KekkeiUnlock
	local KekkeiLocked = stats.KekkeiIsLocked
	local ElementOne = stats.ElementOne
	local ElementTwo = stats.ElementTwo
	local ElementThree = stats.ElementThree
	local UiDone = stats.UiDone
	
	local save = {
		["Village"] = village.Value;
		["FirstName"] = firstName.Value;
		["LastName"] = lastName.Value;
		["Rank"] = rank.Value;
		["KekkeiGenkai"] = KekkeiGenkai.Value;
		["KekkeiUnlocked"] = KekkeiUnlock.Value;
		["KekkeiLocked"] = KekkeiLocked.Value;
		["ElementOne"] = ElementOne.Value;
		["ElementTwo"] = ElementTwo.Value;
		["ElementThree"] = ElementThree.Value;
		["UiDone"] = UiDone.Value;
		
	}

	
	return save
end



function OnRemove(player)
	local data = getSaveTable(player)
	for i,v in pairs(data)do
		print(i,v)
	end
	
	local success,err = pcall(function()
		upg:SetAsync(player.UserId,data)
	end)
	
	if success then
		warn(player.Name..' Stats have been saved')
	elseif err then
		warn(err)
	end
end

game.Players.PlayerAdded:Connect(OnAdded)
game.Players.PlayerRemoving:Connect(OnRemove)
game:BindToClose(function()
	for i,v in pairs(game.Players:GetPlayers()) do
		OnRemove(v)
	end
end)
1 Like

What line is this occurring on in your code? If possible please provide a line number and quote the line for easy access.
Thanks.

the error occurs on line 155, and is referenced with a comment right after the line of code
image

1 Like

Just from a quick look, try changing ["KekkeiUnlocked"] on line 184 to ["KekkeiUnlock"]. Line 155 looks to be attempting to index KekkeiUnlock from the data but it is not set, meaning it returns a null value.

ah i did not notice this even after looking over it so many times being confused

1 Like

Hopefully that’s what is wrong, otherwise I’ll have another look.

i stg man it’s always a spelling error, im just gonna pwn myself now

1 Like

Honestly you’d be surprised how many times something like this has happened to me. Don’t sweat about it. Happy coding!

1 Like