Value is equal to nil?

So I’m a little confused about why I got an error with my script. I was trying to make a system that only takes away money if a player has enough to purchase an upgrade (for a tycoon), but the error is saying ServerScriptService.MainScript:49: attempt to perform arithmetic (sub) on number and nil. Here’s the part of the script that I’m referencing on the erroring line (the reference that’s erroring is the dictionary on the first line given, and the if statement is the line at the end.):

local buttonPrices = {
	StartTycoonButton,startButtonBought = 1000000,false
}
--For every button, do
for i, v in pairs(workspaceChildren) do
	if v:IsA("Model") and string.find(v.Name,"Tycoon") ~= nil then
		local buttonsModelChildren = v.Buttons:GetChildren()
		for i, v in pairs (buttonsModelChildren) do
--If button is touched, perform actions for that specific button
			v.Touched:Connect(function(partThatTouched)
				--If it's the start tycoon button, then
				if string.find(v.Name,"StartTycoonButton") ~= nil then
					if partThatTouched.Parent.Humanoid ~= nil then
						local playerThatTouchedId = game.Players:GetUserIdFromNameAsync(partThatTouched.Parent.Name)
						if playerThatTouchedId == v.Parent.Parent.Misc.OwnerDoor.MainBlock.CurrentOwnerId.Value then
							--Subtract money if player has enough money to purchase
							local playerThatOwnsTycoonString = game.Players:GetNameFromUserIdAsync(v.Parent.Parent.Misc.OwnerDoor.MainBlock.CurrentOwnerId.Value)
							if game.Players[playerThatOwnsTycoonString].leaderstats.Money.Value - buttonPrices[StartTycoonButton] >=0 then

On this line:

Try this:

if game.Players[playerThatOwnsTycoonString].leaderstats.Money.Value - buttonPrices[1] >=0 then

@XdJackyboiiXd21 I did

 if game.Players[playerThatOwnsTycoonString].leaderstats.Money.Value - buttonPrices[1] >=0 then

and got the same error

The problem is that it is getting the following as nil. I just don’t know why that is the case.

@XdJackyboiiXd21 I think it’s because there are multiple variables being defined on the same line. Have you ever done this and read from it successfully?

No I never have, try defining each value on it’s own line. Also is “StartTyCoonButton” a variable, or are you just using that as a string.

local buttonPrices = {
	StartTycoonButton = 1000000,
        startButtonBought = false
}

@XdJackyboiiXd21 I’m trying to have the dictionary define all different buttons that will be in the game, so each button has a number value associated with them.

I see, well maybe after you define the table try adding some prints and see which ones print correctly so you know how to define it later. For example:

print(buttonPrices[1]) --if this prints then use it later to call the number from the table
print(buttonPrices[StartTycoonButton]) --if this prints then use it later to call it from the table

I think either one works, but you need to use quotation marks around the name if you’re trying to use the name. At least that’s what I was seeing from the devhub page about tables.

Yeah that makes sense. I have to go, good luck!

Well I sort of figured it out on my own and it works now.

1 Like