Rebirth System Help

Hello! I have a rebirth system in my game Find The Flags which requires you to have 15 flags at the minimum for your first rebirth. The more rebirths you have the number gets bigger, the max being 8 Rebirths. Now while the entire rebirth system is very broken, I have no idea how to make it better or fix it.

I also need to make it so that specific “event flags” which have a BoolValue inside of them named “NoRebirth” so I can identify which flags don’t get removed when you rebirth. They also don’t count as a flag to rebirth. I’ll be leaving both server and client side scripts in this forum.

Any help is appreciated <3

Server: (This script doesn’t really have issues i just don’t understand how to do the Event Flag thingy)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvents = ReplicatedStorage:WaitForChild("RemoteEvent")
local Event = RemoteEvents:WaitForChild("RebirthEvent")

Event.OnServerEvent:Connect(function(Player)
	
	local leaderstats = Player:WaitForChild("leaderstats")
	local Flags = leaderstats:WaitForChild("Flags")
	local Rebirths = leaderstats:WaitForChild("Rebirths")
	local FlagsCollected = Player:WaitForChild("FlagsCollected")
	
	for i, v in pairs(FlagsCollected:GetChildren()) do
		v:Destroy()
	end
	
	Flags.Value = 0
	Rebirths.Value += 1
	
end)

game.Players.PlayerAdded:Connect(function(Player)
	Event:FireClient(Player)
end)

Client:

repeat task.wait() until game:IsLoaded()

local Player = game.Players.LocalPlayer
local leaderstats = Player:WaitForChild("leaderstats")
local Rebirths = leaderstats:WaitForChild("Rebirths")
local Bar = script.Parent:FindFirstChild("BarBackground").Bar
local Flags = Player:WaitForChild("leaderstats").Flags
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvents = ReplicatedStorage:WaitForChild("RemoteEvent")
local Event = RemoteEvents:WaitForChild("RebirthEvent")

local FlagsCollected = Player:WaitForChild("FlagsCollected")

local FlagsSubtractAmount = 0

local FlagsAmount = 0

local function UpdateFlagsAmount()
	for i,v in pairs(FlagsCollected:GetDescendants()) do
		if v.Name == "NoRebirth" then
			FlagsSubtractAmount += 1
		end
	end
end

local Progress = script.Parent:WaitForChild("Progress")
local Button = script.Parent:WaitForChild("RebirthButton")

local Percentage = script.Parent.Percentage	
local AmountToRebirth = 0

function UpdateRebirthAmount()
    AmountToRebirth = 15 * math.max(Rebirths.Value, 1)
end

function UpdateBar()

	local percentage = math.round(Flags.Value / AmountToRebirth * 100)
	Percentage.Text = percentage.."%"
	Bar:TweenSize(UDim2.new(math.clamp(Flags.Value / AmountToRebirth, 0, 1), 0, 1, 0), "Out", "Quad")
	Progress.Text = Flags.Value.."/"..AmountToRebirth
	
	if Rebirths.Value == 8 then
		Progress.Text = "You have reached the max amount of rebirths."
		Button.Visible = false
	end
	
end

UpdateFlagsAmount()
UpdateRebirthAmount()
UpdateBar()

print(FlagsAmount)
print(Flags.Value)
FlagsAmount = Flags.Value - FlagsSubtractAmount

print(FlagsSubtractAmount)

Event.OnClientEvent:Connect(function()
	UpdateRebirthAmount()
	UpdateBar()
	UpdateFlagsAmount()
end)

Flags.Changed:Connect(function()
	UpdateBar()
	UpdateFlagsAmount()
end)

Rebirths.Changed:Connect(function()
	UpdateRebirthAmount()
	UpdateBar()
	UpdateFlagsAmount()
end)

Button.Activated:Connect(function()
	if Flags.Value >= AmountToRebirth then
		UpdateBar()
		Event:FireServer()
		UpdateFlagsAmount()
	end
end)
1 Like