My cash counter is at -100

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

this is the button that reduces the cash

crate1.MouseButton1Click:Connect(function()

other code

game.ReplicatedStorage.Money.OnServerEvent:Connect(function(plr)
	plr.leaderstats.Money.Value = plr.leaderstats.Money.Value -100
end)

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats= Instance.new("Folder",plr)
	leaderstats.Name= "leaderstats"

	local Money = Instance.new("NumberValue", leaderstats)
	Money.Name= "Money"
	Money.Parent = leaderstats
	Money.Value = 100
end)

local db = true

script.Parent.MouseButton1Click:Connect(function()
	if db == true then
		db = false
		game.ReplicatedStorage.Money:FireServer()
		wait(1)
		db = true
	end
end)

2 Likes
-- 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
2 Likes

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)
2 Likes
If plr.leaderstats.Money.Value >= 100 then
     plr.leaderstats.Money.Value -= 100

       end
end)

Simple if statement should help with your problems.

(Wrote this in mobile)

1 Like
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)

this should work.

1 Like

I recommend plr.leaderstats.Money.Value -= 100 as it’s faster.

1 Like

sure it is and its more readable lol

1 Like

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

2 Likes

I meant it’s faster like faster to write

1 Like

Make somthin like this

if money - (amount being tooken away) <= 0 then
Money -= anount being tooken away)
Script here
End

Thanks for the solution everyone

Please just do this instead:

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.

1 Like