Error when checking for nil

I am trying to make a code system for the first time but i am getting:
“Line 21: Attempt to index nil with ‘StartTime’”

 local DS = game:GetService("DataStoreService")
local Codes = DS:GetDataStore("CodesTest")

local testCode = {
	["Yes"] = {
		StartTime = 0,
		EndTime = 0,
		RewardType = "Skin",
		RewardItem = "YourMom",
		Expired = false
	}
}

game.Players.PlayerAdded:Connect(function(plr)
	local codeInfo
	local s , e = pcall(function()
		codeInfo = Codes:GetAsync("123aa")
	end)

	if s then
		if codeInfo.StartTime then
			print("yes code valid")
		else
			print("code not valid")
		end
	end

	if e then
		print(e)
	end
end)
1 Like

You’re saving the “testCode” dictionary, you also have to access it to then access [‘Yes’], then access its values.

The right way to do that would be:

codeInfo.Yes.StartTime

ah, thank you so much :wink:

1 Like

Here’s also another way to do it:


local Ok, Result = pcall(function()
	return Key:GetAsync('Whatever')
end)

if Ok then
	print(Result.Yes.StartTime)
end
2 Likes