-- Client remotes

replicatedStorage.ClientRemotes.EnemyDied.OnClientEvent:Connect(function(enemy)
	local function GetPlayerRootPart()
		if players.LocalPlayer.Character then
			return players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart")
		end
	end
	wait(0.5)
	for _,part in pairs(enemy:GetDescendants()) do
		if part:IsA("BasePart") or part.ClassName == "Decal" then
			game:GetService("TweenService"):Create(part, TweenInfo.new(1), {Transparency = 1}):Play()
		end
	end
	if enemy:FindFirstChild("LootTags") and enemy.LootTags:FindFirstChild(players.LocalPlayer.Name) and replicatedStorage.ServerSettings.XPCollectEffect.Value == true then
		-- XP effect
		local rootPart = enemy:FindFirstChild("HumanoidRootPart")
		if rootPart then
			local orbs = {}
			for _ = 1,math.random(4,8) do
				local orb = script:WaitForChild("XPOrb"):Clone()
				orb.CFrame = rootPart.CFrame + Vector3.new(math.random(-3, 3), 0, math.random(-3, 3))
				orb.Parent = workspace
				table.insert(orbs, orb)
			end
			wait(1)
			for _,orb in pairs(orbs) do
				if orb and orb.Parent then
					local playerRootPart = GetPlayerRootPart()
					if playerRootPart then
						require(playSoundEffect)("XP")
						orb.Anchored = true
						orb.CanCollide = false
						game:GetService("TweenService"):Create(orb, TweenInfo.new(0.1), {CFrame = playerRootPart.CFrame}):Play()
						wait(0.1)
					end
					orb:Destroy()
				end
			end
		end
	end
end)

replicatedStorage.ClientRemotes.DamageDone.OnClientEvent:Connect(function(enemy, damage)
	local tool = FindToolInHand()
	if enemy:FindFirstChild("HumanoidRootPart") and enemy:FindFirstChild("EnemyHumanoid") and tool then
		require(updateEnvironment).PlaySoundInCharacter(tool.DamageSound.Value)
		local indicator = Instance.new("BillboardGui")
		indicator.LightInfluence = 0
		indicator.AlwaysOnTop = true
		indicator.MaxDistance = 30
		indicator.Size = UDim2.new(5, 0, 1, 0)
		indicator.Parent = enemy.HumanoidRootPart
		local label = Instance.new("TextLabel")
		label.Font = Enum.Font.SourceSansBold
		label.BackgroundTransparency = 1
		label.TextScaled = true
		label.Size = UDim2.new(1, 0, 1, 0)
		label.TextColor3 = Color3.fromRGB(255, 255, 255)
		label.Text = damage
		label.Parent = indicator
		if damage > enemy.EnemyHumanoid.MaxHealth / 3 then -- really good damage
			label.TextColor3 = Color3.fromRGB(255, 0, 0)
		elseif damage > enemy.EnemyHumanoid.MaxHealth / 6 then -- pretty good damage
			label.TextColor3 = Color3.fromRGB(255, 255, 0)
		end
		game:GetService("TweenService"):Create(indicator, TweenInfo.new(0.5), {StudsOffset = Vector3.new(0, 1, 0)}):Play()
		local bodyParts = {}
		for _,part in pairs(enemy:GetDescendants()) do
			if part:IsA("BasePart") then
				table.insert(bodyParts, {part, part.Material, part.Color})
				part.Material = Enum.Material.Neon
				part.Color = Color3.fromRGB(255, 0, 0)
			end
		end
		wait(0.2)
		for _,part in pairs(bodyParts) do
			if part[1] and part[1].Parent then
				part[1].Material = part[2]
				part[1].Color = part[3]
			end
		end
		wait(0.3)
		game:GetService("TweenService"):Create(label, TweenInfo.new(1, Enum.EasingStyle.Linear), {TextTransparency = 1}):Play()
		game:GetService("Debris"):AddItem(indicator, 1)
	end
end)

replicatedStorage.ClientRemotes.TeleportToArea.OnClientEvent:Connect(function(areaName)
	require(updateEnvironment).LoadArea(areaName)
end)
