-- Load default environment

if workspace.Areas:FindFirstChildOfClass("Folder") then
	require(updateEnvironment).LoadArea(workspace.Areas:FindFirstChildOfClass("Folder").Name)
end

game:GetService("RunService"):BindToRenderStep("UI", 50, function() -- Update and animate health UI
	if players.LocalPlayer.Character and players.LocalPlayer.Character:FindFirstChild("Humanoid") then
		if players.LocalPlayer.Character.Humanoid.Health/players.LocalPlayer.Character.Humanoid.MaxHealth >= 0.998 then
			healthFill.UIGradient.Transparency = NumberSequence.new({NumberSequenceKeypoint.new(0, 0), NumberSequenceKeypoint.new(1, 0)})
		elseif players.LocalPlayer.Character.Humanoid.Health/players.LocalPlayer.Character.Humanoid.MaxHealth <= 0.001 then
			healthFill.UIGradient.Transparency = NumberSequence.new({NumberSequenceKeypoint.new(0, 1), NumberSequenceKeypoint.new(1, 1)})
		else
			healthFill.UIGradient.Transparency = NumberSequence.new({NumberSequenceKeypoint.new(0, 0), NumberSequenceKeypoint.new(players.LocalPlayer.Character.Humanoid.Health/players.LocalPlayer.Character.Humanoid.MaxHealth, 0), NumberSequenceKeypoint.new(math.clamp((players.LocalPlayer.Character.Humanoid.Health/players.LocalPlayer.Character.Humanoid.MaxHealth)+0.001, (players.LocalPlayer.Character.Humanoid.Health/players.LocalPlayer.Character.Humanoid.MaxHealth)+0.001, 0.999), 1), NumberSequenceKeypoint.new(1, 1)})
		end
		healthFill.Parent.LifeLabel.Text = math.floor(players.LocalPlayer.Character.Humanoid.Health)
		healthFill.UIGradient.Rotation = healthFill.UIGradient.Rotation - 1
		healthFill.Rotation = healthFill.Rotation + 1
		if lastHealth > players.LocalPlayer.Character.Humanoid.Health then
			DamageEffect(lastHealth/players.LocalPlayer.Character.Humanoid.MaxHealth, (lastHealth-players.LocalPlayer.Character.Humanoid.Health)/players.LocalPlayer.Character.Humanoid.MaxHealth, lastHealth-players.LocalPlayer.Character.Humanoid.Health)
		end
		lastHealth = players.LocalPlayer.Character.Humanoid.Health
	end
	-- Update other stuff in UI
	if players.LocalPlayer.Stats.Rank.Value >= rankCap.Value then
		rankLabel.TextColor3 = Color3.fromRGB(255, 255, 0)
	else
		rankLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
	end
	if players.LocalPlayer.Stats.Mana.Value >= GetManaCap() then
		manaLabel.TextColor3 = Color3.fromRGB(255, 255, 0)
	else
		manaLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
	end
	rankLabel.Text = players.LocalPlayer.Stats.Rank.Value
	goldLabel.Text = players.LocalPlayer.Stats.Gold.Value
	arrowsLabel.Text = players.LocalPlayer.Stats.Arrows.Value
	manaLabel.Text = players.LocalPlayer.Stats.Mana.Value
	rankBar.Parent.Label.Text = players.LocalPlayer.Stats.XP.Value .. "/" .. replicatedStorage.ServerSettings.XPPerRank.Value * players.LocalPlayer.Stats.Rank.Value .. " XP"
	rankBar.Position = UDim2.new((players.LocalPlayer.Stats.XP.Value / (replicatedStorage.ServerSettings.XPPerRank.Value * players.LocalPlayer.Stats.Rank.Value)) - 1, 0, 0, 0)
	rankBar.Fill.Position = UDim2.new(1 - players.LocalPlayer.Stats.XP.Value / (replicatedStorage.ServerSettings.XPPerRank.Value * players.LocalPlayer.Stats.Rank.Value), 0, 0, 0)
end)
