Issues with Moneydrop System

I’ve been making my own wallet system recently but I have a glitch that I’m having a hard time to fix. I’m a bit lightheaded from thinking from all of the possible fixes so apologies for any mistakes. I tend to only use DevForum as a last resort.

So the glitch is when I drop a negative number amount of cash, e.g: -1000$

image

It adds the cash back while dropping a negative number of cash (which if the player decides not to pick, can be used as an infinite money glitch) P.S: I had around 2,000$ before dropping

I assume this could be fixed if I can detect whether the value is negative or not, but I’m not entirely sure how I’d implement this.
This if statement checks if the money value is greater than 0, so I’m confused.

image

Any questions or help will appreciated, thanks!

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Tool = script.Parent
local Handle = Tool.Handle
local MoneyGui = Handle.MoneyGui
local MoneyLabel = MoneyGui.MoneyLabel
local DropMoney = game:GetService("ReplicatedStorage").RemoteEvents.DropMoney
local formatNumber = (function (n)
	n = tostring(n)
	return n:reverse():gsub("%d%d%d", "%1,"):reverse():gsub("^,", "")
end)


Tool.Equipped:Connect(function()
	local Character = Tool.Parent
	local Player = Players:GetPlayerFromCharacter(Character)
	local Money = Player.Money
	
	MoneyLabel.Text = formatNumber(tostring(Money.Value)).."$"
end)

Tool.Activated:Connect(function()
	local Character = Tool.Parent
	local Player = Players:GetPlayerFromCharacter(Character)
	local Money = Player.Money
	DropMoney:FireClient(Player)
end)

DropMoney.OnServerEvent:Connect(function(Player, DropAmount)
	print("fired server")
	local Money = Player.Money.Value
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local moneymodules = ServerStorage.MoneyModules
	local moneybag_min = 2500
	if DropAmount >= moneybag_min and Money > DropAmount and Money > 0 then
		-- Moneybag B)
		print("moneybag", tostring(DropAmount).."$")
		local moneybag = moneymodules.Moneybag:Clone()
		moneybag.Parent = workspace
		moneybag.Position = Character.HumanoidRootPart.Position + Vector3.new(0,0,-2)
		Player.Money.Value -= DropAmount
		moneybag.MoneyGui.MoneyLabel.Text = formatNumber(DropAmount).."$"
	elseif DropAmount < moneybag_min and Money > DropAmount and Money > 0 then
		-- Money
		print("money", tostring(DropAmount).."$")
		local money = moneymodules.Money:Clone()
		money.Parent = workspace
		money.Position = Character.HumanoidRootPart.Position + Vector3.new(0,0,-2)
		Player.Money.Value -= DropAmount
		money.MoneyGui.MoneyLabel.Text = formatNumber(DropAmount).."$"
	end
end)

So first, why are you trying to add a negative number?
Simply make it so you can’t drop a negative number

Have you considered using math.max(0, DropAmount) to prevent negative numbers?

math.max will make it so it will pick 0 the larger number than the negative number effectively removing negative drop amounts.

I’m trying to prevent it so I can’t drop a negative number

Okay. the. Do what @dthecoolest said

I added the math.max() to the DropAmount Variable but it still gives the previous same result.

You need:

math.max(0, DropAmount)

Note the zero value inthe first parameter, or else it will return the same number.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.