This code feels yucky. Is there a better way than to constantly spam FindFirstChild then if thing?
requests.PurchaseRequest.OnServerInvoke = function(player, item: string)
local data = player:FindFirstChild("Data")
if not data then
return "Error", "Something went wrong. Try again!"
end
local permanent_stats = data:FindFirstChild("LifetimeStats")
if not permanent_stats then
return "Error", "Something went wrong. Try again!"
end
local cash = data:FindFirstChild("Tix")
if not cash then
return "Error", "Something went wrong. Try again!"
end
local weapon = weapon_data:FindFirstChild(item)
if not weapon then
return "Error", "Invalid weapon. Hey, this shouldn't happen. Are you cheating?"
end
end
Try using or to make an if statement fire if one of any of the conditions are not met
Also separate your variables from statements to make things more readable / organised. you can keep them at the top unless they’re referred to later on in the script.
requests.PurchaseRequest.OnServerInvoke = function(player, item: string)
local data = player:FindFirstChild("Data")
local permanent_stats = data:FindFirstChild("LifetimeStats")
local cash = data:FindFirstChild("Tix")
local weapon = weapon_data:FindFirstChild(item)
if not data or not permanent_stats or not cash then
return "Error, Something went wrong. Try Again!"
else
if not weapon then
return "Error", "Invalid weapon. Hey, this shouldn't happen. Are you cheating?"
end
end
end)
FindFirstChild helps in making sure things actually exist, if you want to avoid using them then you’ll want to ensure that what vales you’re checking will exist when needed. If you are uncertain, that is when you use FindFirstChild
To add to this, this is an example of a Dictionary in use:
requests.PurchaseRequest.OnServerInvoke = function(player, item: string)
local Dict= {
data = player:FindFirstChild("Data")
permanent_stats = data:FindFirstChild("LifetimeStats")
cash = data:FindFirstChild("Tix")
weapon = weapon_data:FindFirstChild(item)
}
if not Dict["Data"] or not Dict["permanent_stats"] or not Dict["cash"] then
return "Error, Something went wrong. Try Again!"
else
if not Dict["weapon"] then
return "Error", "Invalid weapon. Hey, this shouldn't happen. Are you cheating?"
end
end
end)