my cash counter is at -100 when i click a button it reduces my cash what i wanna do is if player doesnt have enough cash it will not allow to click the button
local db = true
script.Parent.MouseButton1Click:Connect(function()
if db == true then
db = false
game.ReplicatedStorage.Money:FireServer()
wait(1)
db = true
end
end)
-- Probably the most basic math you can do...
if CurrentAmount >= SufficientAmount then
print("Sufficient Amount")
else
warn("Insufficient Amount")
end
-- if the number is greater than or equal to, its Sufficient
-- if not, Its Insufficient
-- use an if statement to check for this condition to confirm this
-- then fire the nessecary code
I think you mean >= …
You can do a simple check like this:
game.ReplicatedStorage.Money.OnServerEvent:Connect(function(plr)
if plr.leaderstats.Money.Value >= 100 then
plr.leaderstats.Money.Value -= 100
end
end)
game.ReplicatedStorage.Money.OnServerEvent:Connect(function(plr)
if plr.leaderstats.Money.Value >= 100 then
plr.leaderstats.Money.Value = plr.leaderstats.Money.Value -100
end
end)
That is the kind of optimization that, in the case it actually is faster, wont lead to any noticeable improvement overall as it’s pretty much negligible compared to everything else.
I do recommend using -=, but for readability
This video goes deep into premature optimization, the trade offs of heavily optimized code, and the method that should be used to optimize code, it’s really a good video
game.ReplicatedStorage.Money.OnServerEvent:Connect(function(plr, amount) -- rename this remote as "UseMoney" for better navigation in the future
local leaderstats = plr:FindFirstChild("leaderstats") -- leaderstats might not exist?
if not leaderstats then
return -- dont do the process
end
local oldValue = plr.leaderstats.Money.Value
plr.leaderstats.Money.Value = math.max(oldValue - (amount or 100), 0)
end)
A second parameter amount is defined to know how much are we going to spend, consume, use or anything in those words of your application. Next, the function math.max is used to prevent the Money value from going below than 0. As for the (amount or 100), in any scenario where amount is not provided, a default value of x or let’s say 100 as given will be used instead.