-- GUI
local lastHealth = 0
local hud = gameUI:WaitForChild("HUD")
local goldLabel = hud:WaitForChild("GoldLabel")
local goldIncreaseLabel = hud:WaitForChild("GoldIncreaseLabel")
local rankLabel = hud:WaitForChild("Rank"):WaitForChild("Rank")
local defenseLabel = hud:WaitForChild("Defense"):WaitForChild("Defense")
local arrowsLabel = hud:WaitForChild("Arrows"):WaitForChild("Arrows")
local manaLabel = hud:WaitForChild("Mana"):WaitForChild("Mana")
local healthFill = hud:WaitForChild("Health"):WaitForChild("Fill")
healthFill.Parent:WaitForChild("LifeLabel")
healthFill:WaitForChild("UIGradient")
local rankBar = hud:WaitForChild("RankBar"):WaitForChild("FillCutoff")
rankBar:WaitForChild("Fill")
rankBar.Parent:WaitForChild("Label")
local switchMenu = replicatedStorage:WaitForChild("Modules"):WaitForChild("SwitchMenu")

function DamageEffect(originalPercentage, damagePercentage, amount)
	require(playSoundEffect)("Hurt" .. math.random(1,2))
	local effect = Instance.new("Frame")
	effect.BorderSizePixel = 0
	effect.Size = UDim2.new(1, 0, damagePercentage, 0)
	effect.Position = UDim2.new(0, 0, 1 - originalPercentage, 0)
	effect.ZIndex = 3
	effect.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
	effect.Parent = healthFill.Parent
	game:GetService("TweenService"):Create(effect, TweenInfo.new(0.3), {BackgroundColor3 = Color3.fromRGB(255, 0, 0), BackgroundTransparency = 1}):Play()
	game:GetService("Debris"):AddItem(effect, 0.5)
	local indicator = Instance.new("TextLabel")
	indicator.Font = Enum.Font.SourceSansBold
	indicator.BackgroundTransparency = 1
	indicator.TextScaled = true
	indicator.TextColor3 = Color3.fromRGB(255, 255, 255)
	indicator.TextStrokeTransparency = 0
	indicator.Text = "-" .. math.floor(amount)
	indicator.TextColor3 = Color3.fromRGB(255, 0, 0)
	indicator.Size = UDim2.new(1, 0, 0.35, 0)
	indicator.Position = UDim2.new(1, 0, 0.325, 0)
	indicator.Parent = healthFill.Parent
	game:GetService("TweenService"):Create(indicator, TweenInfo.new(0.75, Enum.EasingStyle.Linear), {Position = indicator.Position - UDim2.new(0, 0, 0.35, 0), TextTransparency = 1, TextStrokeTransparency = 1}):Play()
	game:GetService("Debris"):AddItem(indicator, 1)
end

function RankUpEffect()
	local rankUpGlow = rankLabel.Parent:WaitForChild("RankUp")
	if rankUpGlow.ImageTransparency == 1 then
		require(playSoundEffect)("RankUp")
		game:GetService("TweenService"):Create(rankUpGlow, TweenInfo.new(0.001), {Size = UDim2.new(1, 0, 1, 0), Position = UDim2.new(0, 0, 0, 0), Rotation = 0}):Play()
		wait()
		game:GetService("TweenService"):Create(rankUpGlow, TweenInfo.new(0.3, Enum.EasingStyle.Linear), {Size = UDim2.new(3, 0, 3, 0), Position = UDim2.new(-1, 0, -1, 0), Rotation = 40, ImageTransparency = 0}):Play()
		wait(0.3)
		game:GetService("TweenService"):Create(rankUpGlow, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {Size = UDim2.new(6, 0, 6, 0), Position = UDim2.new(-2.5, 0, -2.5, 0), Rotation = 80, ImageTransparency = 1}):Play()
	end
end

function XPIncreaseEffect(amount)
	local glow = rankBar.Parent:WaitForChild("Glow")
	local increase = rankBar.Parent:WaitForChild("Increase")
	increase.Text = "+" .. amount
	game:GetService("TweenService"):Create(glow, TweenInfo.new(0.001), {ImageTransparency = 0}):Play()
	game:GetService("TweenService"):Create(increase, TweenInfo.new(0.001), {TextTransparency = 0, TextStrokeTransparency = 0}):Play()
	wait()
	game:GetService("TweenService"):Create(glow, TweenInfo.new(0.3, Enum.EasingStyle.Linear), {ImageTransparency = 1}):Play()
	game:GetService("TweenService"):Create(increase, TweenInfo.new(3, Enum.EasingStyle.Linear), {TextTransparency = 1, TextStrokeTransparency = 1}):Play()
end

function GoldIncreaseEffect(amount)
	goldIncreaseLabel.Text = "+" .. amount
	game:GetService("TweenService"):Create(goldIncreaseLabel, TweenInfo.new(0.001), {TextTransparency = 0, TextStrokeTransparency = 0}):Play()
	wait()
	game:GetService("TweenService"):Create(goldIncreaseLabel, TweenInfo.new(3, Enum.EasingStyle.Linear), {TextTransparency = 1, TextStrokeTransparency = 1}):Play()
end

function UpdateDefense()
	local armor = replicatedStorage.Armor:FindFirstChild(players.LocalPlayer.Stats.CurrentArmor.Value)
	if armor then
		defenseLabel.Text = armor.Defense.Value
	else
		defenseLabel.Text = "0"
	end
end

hud:WaitForChild("Armor").MouseButton1Click:Connect(function()
	require(switchMenu)("ArmorEquipMenu")
end)

hud:WaitForChild("Attributes").MouseButton1Click:Connect(function()
	require(switchMenu)("AttributesMenu")
end)

hud:WaitForChild("Spawn").MouseButton1Click:Connect(function()
	local areaName = workspace.Remotes.ReturnToSpawn:InvokeServer()
	if areaName then
		require(updateEnvironment).LoadArea(areaName)
	end
end)

hud:WaitForChild("Quests").MouseButton1Click:Connect(function()
	require(switchMenu)("QuestsMenu")
end)

function GetManaCap()
	local serverSettings = game:GetService("ReplicatedStorage").ServerSettings
	return serverSettings.StartingMana.Value + (serverSettings.ManaBoostPerMythicality.Value * players.LocalPlayer.Attributes.Mythicality.Value)
end

local rankCap = game:GetService("ReplicatedStorage").ServerSettings.RankCap
local gold = players.LocalPlayer:WaitForChild("Stats"):WaitForChild("Gold")
local xp = players.LocalPlayer:WaitForChild("Stats"):WaitForChild("XP")
local rank = players.LocalPlayer:WaitForChild("Stats"):WaitForChild("Rank")
local currentArmor = players.LocalPlayer:WaitForChild("Stats"):WaitForChild("CurrentArmor")
local prevGold = gold.Value
local prevXP = xp.Value
local prevRank = rank.Value
gold.Changed:Connect(function()
	if gold.Value > prevGold then
		GoldIncreaseEffect(math.floor(gold.Value - prevGold))
	end
	prevGold = gold.Value
end)
xp.Changed:Connect(function()
	if xp.Value > prevXP then
		XPIncreaseEffect(math.floor(xp.Value - prevXP))
	end
	prevXP = xp.Value
end)
rank.Changed:Connect(function()
	if rank.Value > prevRank then
		RankUpEffect()
	end
	prevRank = rank.Value
end)
UpdateDefense()
currentArmor.Changed:Connect(UpdateDefense)