Shorting a function

Hi! Is there any way I can make this code shorter? If i have to make 100 rebirth i ll go crazy:

local Owner = script.Parent.Owner
local CurrentPlr = nil
local CurrentPlrId = 0
local TycoonTeamColor = script.Parent.TeamColor
local cooldown = 0.01

Owner.Changed:Connect(function()
	if Owner.Value ~= nil then
		repeat
			wait(cooldown)
			for i, v in pairs(game:GetService("Players"):GetChildren()) do
				if v.TeamColor == TycoonTeamColor.Value then
							local rebirths = v.leaderstats.Rebirths.Value
							local currencybefore = script.Parent.CurrencyBefore
					local currencycollect = script.Parent.CurrencyToCollect
					while true do
						wait(2)
						if currencybefore.Value ~= 0 then
							if rebirths == 0 then
								currencybefore.Value = currencybefore.Value *1
								currencycollect.Value += currencybefore.Value
								currencybefore.Value = 0
							elseif rebirths == 1 then
								currencybefore.Value = currencybefore.Value *1.2
								currencycollect.Value += currencybefore.Value
								currencybefore.Value = 0
							elseif rebirths == 2 then
								currencybefore.Value = currencybefore.Value *1.4
								currencycollect.Value += currencybefore.Value
								currencybefore.Value = 0
							elseif rebirths == 3 then
								currencybefore.Value = currencybefore.Value *1.6
								currencycollect.Value += currencybefore.Value
								currencybefore.Value = 0
						elseif rebirths == 4 then
								currencybefore.Value = currencybefore.Value *1.8
								currencycollect.Value += currencybefore.Value
								currencybefore.Value = 0
							elseif rebirths == 5 then
								currencybefore.Value = currencybefore.Value *2
								currencycollect.Value += currencybefore.Value
								currencybefore.Value = 0	
							elseif rebirths == 6 then
								currencybefore.Value = currencybefore.Value *2.2
								currencycollect.Value += currencybefore.Value
								currencybefore.Value = 0	
							elseif rebirths == 7 then
								currencybefore.Value = currencybefore.Value *2.4
								currencycollect.Value += currencybefore.Value
								currencybefore.Value = 0
							elseif rebirths == 8 then
								currencybefore.Value = currencybefore.Value *2.6
								currencycollect.Value += currencybefore.Value
								currencybefore.Value = 0
							elseif rebirths == 9 then
								currencybefore.Value = currencybefore.Value *2.8
								currencycollect.Value += currencybefore.Value
								currencybefore.Value = 0
							elseif rebirths == 10 then
								currencybefore.Value = currencybefore.Value *3
								currencycollect.Value += currencybefore.Value
								currencybefore.Value = 0
							elseif rebirths == 11 then
								currencybefore.Value = currencybefore.Value *3.2
								currencycollect.Value += currencybefore.Value
								currencybefore.Value = 0
							elseif rebirths == 12 then
								currencybefore.Value = currencybefore.Value *3.4
								currencycollect.Value += currencybefore.Value
								currencybefore.Value = 0	
							elseif rebirths == 13 then
								currencybefore.Value = currencybefore.Value *3.6
								currencycollect.Value += currencybefore.Value
								currencybefore.Value = 0
							elseif rebirths == 14 then
								currencybefore.Value = currencybefore.Value *3.8
								currencycollect.Value += currencybefore.Value
								currencybefore.Value = 0
							elseif rebirths == 15 then
								currencybefore.Value = currencybefore.Value *4
								currencycollect.Value += currencybefore.Value
								currencybefore.Value = 0
							elseif rebirths == 16 then
								currencybefore.Value = currencybefore.Value *4.2
								currencycollect.Value += currencybefore.Value
								currencybefore.Value = 0
							elseif rebirths == 17 then
								currencybefore.Value = currencybefore.Value *4.4
								currencycollect.Value += currencybefore.Value
								currencybefore.Value = 0
							elseif rebirths == 18 then
								currencybefore.Value = currencybefore.Value *4.6
								currencycollect.Value += currencybefore.Value
								currencybefore.Value = 0
							elseif rebirths == 19 then
								currencybefore.Value = currencybefore.Value *4.8
								currencycollect.Value += currencybefore.Value
								currencybefore.Value = 0
							elseif rebirths == 20 then
								currencybefore.Value = currencybefore.Value *5
								currencycollect.Value += currencybefore.Value
								currencybefore.Value = 0
								end
								end
						end
					end
				end
		until Owner.Value == nil
	end
end)

currencybefore.Value *= (1 + (rebirths * 0.2))

1 Like

He is talking about typing if rebirth == 1,2,3,4
100 times (i think)

Yeah, this one. So I would not put 100 if functions

Maybe

for i = 0, 100, 1 do -- Change 100 to the maxRebirth you want, the 1 is the increment increase
   local currencyIncrement = 1 + i * 0.2

   if rebirths == i then
      currencybefore.Value *= currencyIncrement
      currencycollect.Value += currencybefore.Value
      currencybefore.Value = 0
   end
end

I haven’t tested this out im not sure if it works

1 Like

Yup. This is it, it’s working. Thank you so much! <3

1 Like

No problem i think its better if you add what i edited in the script

If you don’t mind? What it does? The function you added

my bad you dont need to use ill just edit back
But the function .Changed checks if the intValue changed, like if rebirths changed but i didnt see the script above it so no need to add it

1 Like

Alright. Thank you again!

30 limittt

if currencybefore.Value ~= 0 then
	currencybefore.Value = currencybefore.Value*(1+rebirths*0.2)
	currencycollect.Value += currencybefore.Value
	currencybefore.Value = 0
end

This is even simpler, and achieves what you set out to achieve in the code you originally posted.

1 Like

I’ll actually use this, it’s even easier. Thank you guys!

I don’t get why you use += but you never use *=

if currencybefore.Value ~= 0 then
	currencybefore.Value *= (1 + rebirths * 0.2)
	currencycollect.Value += currencybefore.Value
	currencybefore.Value = 0
end

looks a bit better now imo

1 Like

I didn’t even realise “currencybefore.Value” was referenced twice in the same expression, I thought two different values were being fetched.

If you wanted to go 1 step further you could do the following:

local Owner = script.Parent.Owner
local CurrentPlr = nil
local CurrentPlrId = 0
local TycoonTeamColor = script.Parent.TeamColor
local cooldown = 0.01

Owner.Changed:Connect(function()
	if Owner.Value ~= nil then
		repeat
			wait(cooldown)
			for i, v in pairs(game:GetService("Players"):GetChildren()) do
				if v.TeamColor == TycoonTeamColor.Value then
					local rebirths = v.leaderstats.Rebirths.Value
					local currencybefore = script.Parent.CurrencyBefore
					local currencycollect = script.Parent.CurrencyToCollect
					while task.wait() do
						currencybefore.Value *= (1 + rebirths * 0.2)
						currencycollect.Value += currencybefore.Value
						currencybefore.Value = 0
						end
					end
				end
			end
		until Owner.Value == nil
	end
end)

and get rid of any unnecessary conditional checks.

1 Like

Also, I wanted to ask you guys, is there any way to do the same short cut for this too? So I could not add rebirths anymore, maybe the rebirth price will multiply by itself

			['unlockPurchases'] = {
				{rebirthCount = 1,unlock = "rebirth1"},
				{rebirthCount = 2,unlock = "rebirth2"},
				{rebirthCount = 3,unlock = "rebirth3"},
				{rebirthCount = 4,unlock = "rebirth4"},
				{rebirthCount = 5,unlock = "rebirth5"},
				{rebirthCount = 6,unlock = "rebirth6"},
				{rebirthCount = 7,unlock = "rebirth7"},
				{rebirthCount = 8,unlock = "rebirth8"},
				{rebirthCount = 9,unlock = "rebirth9"},
				{rebirthCount = 10,unlock = "rebirth10"},
				{rebirthCount = 11,unlock = "rebirth11"},
				{rebirthCount = 12,unlock = "rebirth12"},
				{rebirthCount = 13,unlock = "rebirth13"},
				{rebirthCount = 14,unlock = "rebirth14"},
				{rebirthCount = 15,unlock = "rebirth15"},
				{rebirthCount = 16,unlock = "rebirth16"},
				{rebirthCount = 17,unlock = "rebirth17"},
				{rebirthCount = 18,unlock = "rebirth18"},
				{rebirthCount = 19,unlock = "rebirth19"},
				{rebirthCount = 20,unlock = "rebirth20"},
				{rebirthCount = 21,unlock = "rebirth21"},
				{rebirthCount = 22,unlock = "rebirth22"},
				{rebirthCount = 23,unlock = "rebirth23"},
				{rebirthCount = 24,unlock = "rebirth24"},
				{rebirthCount = 25,unlock = "rebirth25"},
				{rebirthCount = 26,unlock = "rebirth26"},
				{rebirthCount = 27,unlock = "rebirth27"},
				{rebirthCount = 28,unlock = "rebirth28"},
				{rebirthCount = 29,unlock = "rebirth29"},
				{rebirthCount = 30,unlock = "rebirth30"},
				{rebirthCount = 31,unlock = "rebirth31"}
			},
			
			['defaultCashNeededForRebirth'] = 0,
			['cashNeededForRebirth'] = {
				{rebirthCount = 1,cashAmount = 0},
				{rebirthCount = 2,cashAmount = 150000},
				{rebirthCount = 3,cashAmount = 180000},
				{rebirthCount = 4,cashAmount = 210000},
				{rebirthCount = 5,cashAmount = 260000},
				{rebirthCount = 6,cashAmount = 310000},
				{rebirthCount = 7,cashAmount = 380000},
				{rebirthCount = 8,cashAmount = 450000},
				{rebirthCount = 9,cashAmount = 550000},
				{rebirthCount = 10,cashAmount = 650000},
				{rebirthCount = 11,cashAmount = 770000},
				{rebirthCount = 12,cashAmount = 920000},
				{rebirthCount = 13,cashAmount = 1100000},
				{rebirthCount = 14,cashAmount = 1300000},
				{rebirthCount = 15,cashAmount = 1600000},
				{rebirthCount = 16,cashAmount = 2400000},
				{rebirthCount = 17,cashAmount = 3600000},
				{rebirthCount = 18,cashAmount = 4000000},
				{rebirthCount = 19,cashAmount = 5000000},
				{rebirthCount = 20,cashAmount = 6000000},
				{rebirthCount = 21,cashAmount = 7000000},
				{rebirthCount = 22,cashAmount = 8000000},
				{rebirthCount = 23,cashAmount = 9000000},
				{rebirthCount = 24,cashAmount = 10000000},
				{rebirthCount = 25,cashAmount = 11000000},
				{rebirthCount = 26,cashAmount = 12000000},
				{rebirthCount = 27,cashAmount = 13000000},
				{rebirthCount = 28,cashAmount = 14000000},
				{rebirthCount = 29,cashAmount = 15000000},
				{rebirthCount = 30,cashAmount = 16000000},
				{rebirthCount = 31,cashAmount = 17000000}
			},
for i=1, 31, 1 do
    ["unlockPurchases"][i] = {rebirthCount = i, unlock = "rebirth".. i}
end

Does the second part go up in a calculated amount, or just random?

EDITED.

Oh wait sorry I tought it was for the money. I think that one is good for the first part

PascalCase
local Players = game:GetService("Players")

local Parent = script.Parent
local TycoonTeamColor = Parent.TeamColor
local CurrencyBefore = Parent.CurrencyBefore
local CurrencyCollect = Parent.CurrencyToCollect

local CurrentPlayerId = 0
local CurrentPlayer = nil
local Cooldown = 0.01

Owner.Changed:Connect(function()
    if Owner.Value then
        repeat task.wait(Cooldown)
            for _, Player in ipairs(Players:GetPlayers()) do
                if Player.TeamColor == TycoonTeamColor.Value then
                    local Rebirths = Player.leaderstats.Rebirths

                    CurrencyBefore.Value *= (1 + Rebirths.Value * 0.2)
                    CurrencyCollect.Value += CurrencyBefore.Value
                    CurrencyBefore.Value = 0
                end
            end
        until not Owner.Value
    end
end)
camelCase
local players = game:GetService("Players")

local parent = script.Parent
local tycoonTeamColor = parent.TeamColor
local currencyBefore = parent.CurrencyBefore
local currencyCollect = parent.CurrencyToCollect

local currentPlayerId = 0
local currentPlayer = nil
local cooldown = 0.01

owner.Changed:Connect(function()
    if owner.Value then
        repeat task.wait(cooldown)
            for _, player in ipairs(players:GetPlayers()) do
                if player.TeamColor == tycoonTeamColor.Value then
                    local rebirths = player.leaderstats.Rebirths

                    currencyBefore.Value *= (1 + rebirths.Value * 0.2)
                    currencyCollect.Value += currencyBefore.Value
                    currencyBefore.Value = 0
                end
            end
        until not owner.Value
    end
end)

I made some changes to the code

  1. use PascalCase or camelCase, pick one
  2. use Players:GetPlayers() not Players:GetChildren()
  3. use task.wait() not wait()
  4. what was the point of that while loop, it would go on forever
    I decided to remove it just incase
  5. you should define CurrencyBefore and CurrencyToCollect outside the loop
  6. instead of i, v what about using _, Player
    easier to read now

sorry if it feels like I’m nitpicking now, but I wanted to show this

I believe you should’ve replied to the original poster (or sent him a message), the code outside of the while loop I left untouched.

1 Like