My game has been breaking randomly for the past week, and this is another bug that I haven’t been able to figure out. I use a template system for loading items in the Shop. I tried printing the value before purchasing the item and getting an error, and instead of nil, I see “500”, “20”, etc.
Why is this happening? This worked fine a week ago, and without any changes, suddenly stopped working. Here’s my code:
Client:
local itemName = script.Parent.ItemName.Value
local itemNameInCode = script.Parent.ItemNameInCode.Value
local currencyType = script.Parent.CurrencyType
local currentAmount
if currencyType.Value == "Bucks" then
currentAmount = game.ReplicatedStorage:WaitForChild("Functions").GetCurrency:InvokeServer("Bucks")
elseif currencyType.Value == "Tokens" then
currentAmount = game.ReplicatedStorage:WaitForChild("Functions").GetCurrency:InvokeServer("Tokens")
end
local price = game.ReplicatedStorage:WaitForChild("Functions").GetPrice:InvokeServer(itemNameInCode)
print(tostring(price)) -- doesn't print nil
-- TODO: Handle return false
if (currentAmount >= price) then
game.ReplicatedStorage:WaitForChild("Events"):WaitForChild("BuyItem"):FireServer(itemNameInCode, currencyType.Value) -- this errors
end
Server:
funcs:WaitForChild("GetPrice").OnServerInvoke = function(player, itemName)
for v,i in pairs(ITEM_PRICES) do
if v == itemName then
return i
else
warn("Item price doesn't exist!") -- i don't see this in output
return false
end
end
end
Hi there,
from the title I am really unable to find out what your issue is, but after looking deeper into this, I see you may be facing a problem when currentAmount is a nil, resulting to such an error.
While you did mark different line as the one you have problems with, I see no reason why would that give you an error, unless the BuyItem is not a RemoteEvent.
There’s also one thing I would like to point out - it’s the fact you use :WaitForChild almost completely everywhere you can. While this is somewhat good way to ensure the Instance is in it’s place before you manipulate with it, it is really bad for performance.
If you comment out the line, would the error still happen?
This is not going to fix your code by any means, I would just like to be 100% sure the error comes from this line, because it’s very unrealistic that the error comes from this line of code.
If the error’s origin is actually this line, the error shouldn’t be there if you comment that piece of code out.
This might be really weird approach of debugging, but I tend to do this when I am lost and it works very good.
Maybe your if condition is returning false both times, leaving currentAmount empty? Put a print statement in both and see what happens.
Edit: both the initial check and in the elseif, that is.
Run this a few times to see if anything changes, since you said this script is working inconsistently. Whenever things just stop working out of nowhere, it’s often that pieces aren’t loading in/changing when you need them to asynchronously, and you’ve just never run into it before.
Well, wait a second there. Read my edit; the chances are that you’ve just stopped running into for now. You want to squash this now while it’s still fresh on your mind, because if you didn’t change anything, it will happen again. Like I said, since it’s am inconsistent error, it’s probably some dependency not loading in. Try doing what I suggested, and test it several times. Even if you don’t get the error again, leave those print statements in, so whenever you do get it again, you’ll see what’s happening. You should be doing everything in your power to debug this while it’s still small.