I'm working on a tycoon game and have the basic buying coded how ever I want to have the first button set to be free so i set the price as 0 problem is the equation I have to subtract from players cash it out puts an error about how it can't subtract from 0. So I go to fix the problem but whenever I attempted an If else statement the script freaks out and Multiple errors pop up that don't actually when I investigate them.
#Game keeps trying to subtract 0
1 messages · Page 1 of 1 (latest)
make an if statment
the most stable solution I've found but I'm not sure how to resolve when the Price equal to or less than 0
Can we see those errors?
bro
no reason to have the else there
if i remove the else it breaks something in a completely different place
You dont need it
Then lets see the whole script as something else is broken
Also, why is it bad that it subtracts by 0? It won't change the value
07:35:24.561 ServerScriptService.TycoonHandler:52: attempt to perform arithmetic (sub) on number and nil
that
it only happens if the price is set to 0
If you couldn't subtract by 0 that'd be awful.
Can you print out what value is before you subtract
no, that means you're doing number - nil
which is in fact performing an arithmetic sub on number and nil
it's trying to subtract 0 from the leaderboard, lua can't subtract by 0
no, read the error
there is something in your code that is making that you're subtracting nil to a number
The error does not occur if price value I have set for the button is not zero, if only occurs if the price I have set on the button is zero.
** You are now Level 1! **
that is not what I'm talking about, the error is what it is, and it is saying you're subtracting nil to a number
check that out
Nil is 0
💀
no
nil is not 0, no language does that
0 is by definition not nil
Just give the full code
Have it print(Price) before subtracting it. If that prints Nil, then just add a if not Price then Price = 0 end
** You are now Level 3! **
print(Price) if Price ~= nil then Price = 0 would this work to convert nil to zero
The price subtraction is outside of the check to see if Price (which is an attribute) is even there
That could be why it's subtracting nil
No, as that'd set Price if Price isn't nil
you can just do Price or 0 instead, so if Price is nil, it'd choose 0 instead
putting the equation in there breaks the the spawn code for the buttons
Well where are you putting it and what are you doing with it
You'd get the price like local Price = button:GetAttribute("price") or 0
just tried that, it just prints 0 in output now
But like what are you printing and where? Are you saying no buttons have a price that's not 0?
the one and only button's price is 0 it has been set to this whole time, it for no explainable reason is reading that 0 as nil
okay, so did you use the or 0 thing? If you did, show me the code where you did
ugh I think I found figured it out, lua is case sensitive the number on the button is under price with an uppercase P and it want's price withe a lower case p... im going to do somthing
I don't think I've heard of a programming language where capitalization didn't matter
but yes that's a major issue.
hold on let me test it now that ive fixed that
** You are now Level 2! **
Well does it work
There aren’t any prints in that code, are you sure you’re looking in the right place?
You don't need to check if Price is nil anymore, so you can get rid of the if Price then statement
i look in the output when i test and it just says 0 x amount of times
Do you even know where the print is?
thats not what that was for
The print will probably show what line it’s from, go look there
i somehow fixed it the problem on my own
i undid something and now no errors, no clue what
I hate lua
I mean this is just programming in general lol, you're going to create bugs and fix bugs then make more bugs
instead of buying the free upgrade. why not remove the entire buying idea and just trigger the upgrade right as its touched??