Attempt to index field '?' (a nil value)

Hi, I am trying to make an upgrade system, I put the player’s upgrades into a table and when I go to upgrade the item I get the error, [attempt to index field ‘?’ (a nil value), anybody know what the problem is?

The upgrade table

_G.Data[player.Name]["Upgrades"] = {
		 -- value, valueIncrement, max/min, cost, costIncrement
		["CoinsPerCritical"] = {1, 1, 100, 25, 25};
		["Chance"] = {1, 1, 100, 100, 100};
		}

The upgrade script

functions["Upgrade"].OnServerInvoke = function(player, Info)
	print(_G.Data[player.Name]["Upgrades"][Info.Stat][1])
	if _G.Data[player.Name]["Upgrades"][Info.Stat] then
		if _G.Data[player.Name]["Coins"] >= _G.Data[player.Name]["Upgrades"][Info.Stat][4] then
			_G.Data[player.Name]["Coins"] = _G.Data[player.Name]["Coins"] - _G.Data[player.Name]["Upgrades"][Info.Stat][4]
			_G.Data[player.Name]["Upgrades"][Info.Stat][4] = _G.Data[player.Name]["Upgrades"][Info.Stat][4] + _G.Data[player.Name]["Upgrades"][Info.Stat][5]
			_G.Data[player.Name]["Upgrades"][Info.Stat][1] = _G.Data[player.Name]["Upgrades"][Info.Stat][1] + _G.Data[player.Name]["Upgrades"][Info.Stat][2]
	        player["leaderstats"]["Coins"].Value = _G.Data[player.Name]["Coins"]
	end

	local data = {}

		data["Cost"] = _G.Data[player.Name]["Upgrades"][Info.Stat][4]
		data["UpgradeValue"] = _G.Data[player.Name]["Upgrades"][Info.Stat][1]
		
	return data
	end
end

Which line does the script error, don’t just give us the number, quote the actual line (you might have omitted part of your script)

this is where it is erroring
print(_G.Data[player.Name]["Upgrades"][Info.Stat][1])

this is what the client is sending

for _,v in pairs(script.Parent:GetChildren()) do
	if v:IsA("Frame") then
		v.Buy.Activated:Connect(function()
			if deb == false then deb = true
			local returnedStats = functions["Upgrade"]:InvokeServer({tonumber(v)})
			print(v)
				if v == "Chance" then
					v.Need.Text = returnedStats.UpgradeValue.."% Chance Per Click"
			elseif v == "CoinsPerCritical" then
					v.Need.Text = "+"..returnedStats.UpgradeValue.." Coins Per Click"
			   end
					v.Buy.UpgradeText.Text = "UPGRADE - "..returnedStats.Cost.."Coins"
			wait(0.5)        
			deb = false
			end
			end)
	end
end

Can you try to call Info.Stat as tostring(Info.Stat) please? Also, can you tell me if Stat it’s declared as string value?

it was a mistake, I was suppose to do tonumber(v)

I am still getting ServerScriptService.Server Controller:328: attempt to index field ‘?’ (a nil value)

I didn’t receive an answer on this can you please tell me if this is an Instance or not?

according to typeof(), it is nil

If Stat is nil, the statement with the error should rightfully return a nil. That’s why you’re getting the error.

Okay, what is this Stat variable supposed to do anyways?

I am trying to get the name of the upgrade, so there are frames in the upgrade gui, so when the player clicks one of the frame’s upgrade button it will fire the name of the frame which is one of stats, “Chance” or “CoinsPerCritical”

also changed it back to tostring(), still getting the same error

@JinxFury18 @ForeverHD

You don’t receive a correct value there then if the value it’s nil and it errors there. May I ask why are you Invoking number to server in curly brackets?

I fire arguements into a table so I can just call it Info on the server script,so if I got more arguements to send out I can just do something like Info.FrameName on the serverside

If you send string to server then why do you use tonumber if you get a string instead? Use tostring instead of tonumber then.

now I am getting the error, Players.CleanVac.PlayerGui.MainGui.Frames.Upgrade.UpgradeFrame.LocalScript:17: attempt to index local ‘returnedStats’ (a nil value)

error line
v.Buy.UpgradeText.Text = "UPGRADE - "..returnedStats.Cost.."Coins"

I have changed it to tostring()

Use:

[v.Name].Buy.UpgradeText.Text = "UPGRADE - "..returnedStats.Cost.."Coins"

or

[tostring(v)].Buy.UpgradeText.Text = "UPGRADE - "..returnedStats.Cost.."Coins"

the error is now on the client side, but the server is still returning nil

Make sure you check if returnedStats is not nil before assuming it exists.

local returnedStats = functions["Upgrade"]:InvokeServer({Stat = v.Name})
if returnedStats then
-- Code
end

returnedStats is nil you don’t recieve any value from there? Can you try to call it as:

returnedStats["Cost"][1]

now the server side isnt doing anything anymore

Make sure that when you invoke the server you use a dictionary not an array.

Do this

local returnedStats = functions["Upgrade"]:InvokeServer({Stat = v.Name})

Not this

local returnedStats = functions["Upgrade"]:InvokeServer({v.Name})
1 Like