How would i add a % tax to my money system?

hi!!

im making a basic civilization game, like generic roleplay gaem

ive already made the sysytem for the tax rate being in place, and the rate also changing, but how would i actually implement the tax being added onto the profit of something that a player gains?
assuming they were to make $2, but the tax rate is 25%, they only make 75% of the profit, but im really not sure how to add it.

my code is currently looking like this:

local Players = game:GetService("Players")

local function onAxeTouch(otherPart)
    local log = otherPart.Parent:FindFirstChild("treedetect")
    local treeHealth = log and log:FindFirstChild("TreeHealth")
	local profit = log:FindFirstChild("Profit")
	local taxrate = game.ReplicatedStorage.TaxRateValue.Value
	local moneytogive = profit - (profit*taxrate)
	
    if treeHealth then
        treeHealth.Value = treeHealth.Value - 1
        if treeHealth.Value <= 0 then
            local player = Players:GetPlayerFromCharacter(otherPart.Parent)
            if player then
                local leaderstats = player:FindFirstChild("leaderstats")
                local cash = leaderstats and leaderstats:FindFirstChild("Cash")
                if cash then
                    cash.Value = cash.Value + moneytogive
                end
            end
        end
    end
end

local function setupAxe(axe)
    local handle = axe:FindFirstChild("Handle")
    if handle then
        handle.Touched:Connect(onAxeTouch)
    end
end

however this doesn’t work and just adds the flat value

any responses would be appreciated thanks!!!

shouldn’t you be using profit.Value here?
Anyways, assuming that isn’t the issue, you said that the cash adds the amount before getting taxed, correct?

uhhh, well i think it just adds the original amount without the tax, yeah.
which isnt the intention
also just to note i did have a post before but it went inactive so i made this 1

I’m genuinely puzzled because I can’t think of a way this wouldn’t work, unless I’m overlooking something.
image
image

Even if taxrate was not a decimal, it would still return a different value.
image

I’d like to know what the problem was when you finally figure it out, lol.

1 Like

Oh actually, can you put these 3 lines right after you define moneytogive, and show me what it says?

print(profit)
print(taxrate)
print(moneytogive)

I just want to make sure that the values were all correct like you mentioned in the last post.

3 Likes

yo sorry i was busy, but ill try this soon, or tommorow if i cant get to it. pretty late for me
but i seriously appreciate all the help its very nice!!

will update soon

1 Like

Net Profit = Gross Profit × (1−Tax Rate)

  • Net Profit is the resulting amount after this calculation.
  • Gross Profit is the total profit earned by the player.
  • Tax Rate is the tax rate in decimal form (e.g., 8% is 0.08).

For example, if a player makes a profit of $100 with a tax rate of 8%:
Net Profit = $100×(1−0.08)
= $100×0.92
= $92

oh i know how the whole tax thing works and how im intending for it, but i was referring to the mathematical process in the code, and making it work
mainly because im not too good at coding as is, not to mention the math part of it

thank you though

ok well printing it doesnt quite work well

You’re not extracting the value from Profit, you’re instead multiplying the Instance with a number. @domboss37 mentioned that you probably meant to index the Value of Profit instead of directly doing math with the Instance.

1 Like

(post deleted by author)

i remember reading his comment actually but i completely forget to try it, thanks for the reminder

has this fixed the issue for you?

nah, it mostly did the same thing was before. really confusing

ok, can you snap a picture of the output again to see if there are any more errors

The following function should work:

local function tax(money: number, rate: number): number
	return money*(1-rate) -- x-x*y == x*(1-y)
end

local money = 2 --2$
local rate = 0.25 --25%

local afterTax = tax(money, rate)
print(afterTax) --1.5$

i reckon that would work but the big dilemma here is that the tax rate can be changed at any time by a certain player in the server, up or down by 5%

Just fetch the current tax rate instead of the hardcoded value.

1 Like