Attempt to index nil even though it exists

local myvalues = {}
print("latest")

game.Players.PlayerAdded:Connect(function(player)
	local id = player.UserId

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	myvalues["intvalue"] = Instance.new("IntValue")
	myvalues["intvalue"].Parent = leaderstats
	

	local success,ttable = pcall(function()
		store:GetAsync(id)
	end)
	if success then
		myvalues["intvalue"].Value = ttable["intvalue"].Value or 0
	end
end)

errors on this

myvalues["intvalue"].Value = ttable["intvalue"].Value or 0

ServerScriptService.Script:25: attempt to index nil with ‘intvalue’

You have the ttable when using protective call function and you’re referring it.

local success,ttable = pcall(function()
return store:GetAsync(id)
end)

the second value that is returned by the pcall function is the value returned by the function inside the pcall, or if it fails it returns the error string

2 Likes

May I also suggest noting that Instance.new has a second parameter for where the instance should be parented to which can almost always save you an extra line of code

You could replace

myvalues["intvalue"] = Instance.new("IntValue")
myvalues["intvalue"].Parent = leaderstats

with

myvalues["intvalue"] = Instance.new("IntValue",leaderstats)
1 Like

it still errors, its trying to index myvalues which is nil apprantley , not ttables

what do you mean by that, please be more descriptive

See the variable ttable? You’re indexing ttable, which is nil, from that variable.

you created a table so the error would be attempt to index nil with ‘Value’

probably this

is returning a nil value so make it return a empty table if not find one

return store:GetAsync(id) or {}

1 Like

When a player joins for the first time, ttable will be nil, so you should do a check before using it.

myvalues["intvalue"].Value = ttable and ttable["intvalue"] and ttable["intvalue"].Value or 0
1 Like