Hi,
Can somebody please explain to me why this line isn’t working.
if Cost and bCash.Value >= Cost.Value or wCash.Value >= Cost.Value and not PurchasedCars:FindFirstChild(carname) then
Especially the “not PurchasedCars:FindFirstChild(carname)”
PurchasedCars is a folder inside of ServerStorage>PurchasedCars named by the player name.
carname is a string with car’s name, in this case crownVictoria.
When I printed “PurchasedCars:FindFirstChild(carname)” it showed me the object because it is in that folder but for some reason it still gets past the if check even though it clearly is in that folder so by putting a not condition before it should only go past it if the object is not there.
Try to put the PurchasedCars:FindFirstChild(carname) into brackets like this
not (PurchasedCars:FindFirstChild(carname))
1 Like
Thanks for replying, I tried it, but still no luck.
local function costExists()
print("costExists check")
return true
end
local function enoughBCash()
print("bCash check")
return true
end
local function enoughWCash()
print("wCash check")
return true
end
local function purchasedCar()
print("purchased check")
return true
end
local result = (costExists() and enoughBCash() or enoughWCash() and not purchasedCar())
print("Result:", result)
When we run the above, we can see that purchasedCar is never called, as the statement is already deemed true. This concept is called short-circuiting, where a conditonal statement is no longer evaluated when the result is known.
Why is the result known? Because the statement reads as if (costExists and enoughBCash) OR (enoughWCash and not purchasedCar)
, if costExists returns false, the enoughWCash and purchasedCar would now evaluate (and your program would throw an error, as cost doesn’t exist).
The fix: costExists() and (enoughBCash() or enoughWCash()) and not purchasedCar()
1 Like
Thanks for replying, from what I understand I should make a function for each condition in the if statment like so:
local function enoughWCash()
if wCash.Value >= Cost.Value then
return true
else
return false
end
end
And then in my current if statment put enoughWCash() and enoughBCash() and so on?
I was simply being verbose as an example (especially if you were to run it), this would work:
if Cost and (bCash.Value >= Cost.Value or wCash.Value >= Cost.Value) and not PurchasedCars:FindFirstChild(carname) then
Functions may or may not make sense to be able to quickly parse the line, or be able to logically name the function enoughCash. It’s all preference.
local function enoughCash()
return (bCash.Value >= Cost.Value) or (wCash.Value >= Cost.Value)
end
1 Like
Thank you so much! It works now.