--Sound variables
local runningSound = myHead.Running
local jumpingSound = myHead.Jumping
local hurtSound = myHead.Hurt
local diedSound = myHead.Died

myHuman:SetStateEnabled(Enum.HumanoidStateType.Flying,false)
myHuman:SetStateEnabled(Enum.HumanoidStateType.GettingUp,false)
myHuman:SetStateEnabled(Enum.HumanoidStateType.Physics,false)
myHuman:SetStateEnabled(Enum.HumanoidStateType.Ragdoll,false)
myHuman:SetStateEnabled(Enum.HumanoidStateType.RunningNoPhysics,false)
myHuman:SetStateEnabled(Enum.HumanoidStateType.StrafingNoPhysics,false)
myHuman:SetStateEnabled(Enum.HumanoidStateType.Swimming,false)

local rotAttach1 = Instance.new("Attachment")
rotAttach1.Parent = workspace.Terrain

myHuman.Running:Connect(function(speed)
	if speed>0 then
		runningSound:Play()
	else
		runningSound:Stop()
	end
end)

myHuman.Jumping:Connect(function()
	jumpingSound:Play()
end)

if marine.Settings.PlayDeathAnimation.Value == true then
	myHuman.BreakJointsOnDeath = false
end

myHuman.Died:Connect(function()
	diedSound:Play()
	actions.yieldM4()
	actions.updateFace("Dead")
	if marine.Settings.PlayDeathAnimation.Value == true then
		myHuman.BreakJointsOnDeath = false
		marine.HumanoidRootPart.Anchored = true
		DeathAnimation:Play()
		DeathAnimation.Stopped:Wait()
	end
	for i,v in ipairs(marine:GetDescendants()) do
		if v:IsA("BallSocketConstraint") then
			v.Enabled = true
		elseif v:IsA("BasePart") and v.Name ~= "myHumanoidRootPart" then
			v.CanCollide = false
		elseif v:IsA("Motor6D") then
			v:Destroy()
		end
	end
	if marine.Settings.Respawn.Value then
		wait(marine.Settings.RespawnDelay.Value)
		clone.Parent = marine.Parent
		ArmorDurability = MaxArmorDurability
	end
	for i,v in ipairs(marine:GetDescendants()) do
		if v:IsA("BasePart") or v:IsA("Decal") then
			game:GetService("TweenService"):Create(v,TweenInfo.new(0.2),{Transparency = 1}):Play()
		end
	end
	rotAttach1:Destroy()
	wait(0.2)
	marine:Destroy()
end)

local soundSpeeds = {0.9,0.95,1,1.05,1.1}
myHuman.HealthChanged:Connect(function(health)
	if ArmorDurability > 0 then
		local damageTaken = oldHealth - health
		
		if (ArmorDurability - damageTaken) <= 0 then
			ArmorDurability = 0
			marine.Torso.ArmorBroken:Play()
			
			local Info = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)
			local Properties1 = {BackgroundTransparency = 1}
			local Properties2 = {ImageTransparency = 1}
			
			ArmorFill.Size = UDim2.new(0, 0, 1, 0)
			
			for i,v in pairs(ArmorGUI:GetDescendants()) do
				if v:IsA("Frame") then
					local Tween = TweenService:Create(v,Info,Properties1)
					
					Tween:Play()
				elseif v:IsA("ImageLabel") then
					local Tween = TweenService:Create(v,Info,Properties2)
					
					Tween:Play()
				end
			end
		else
			ArmorDurability -= damageTaken
			
			ArmorFill.Size = UDim2.new((ArmorDurability / MaxArmorDurability) * 1, 0, 1, 0)
			
			myHuman.Health = oldHealth
		end
	end
	
	if health < oldHealth and hurtSound.IsPlaying == false then
		status:set("tookDamage",true)
		if math.random(3) == 1 then
			hurtSound.PlaybackSpeed = soundSpeeds[math.random(#soundSpeeds)]
			hurtSound:Play()
		end
		core.spawn(function()
			actions.updateFace("Hurt")
			wait(1)
			if myHead:FindFirstChild("faceHurt") then
				actions.updateFace(status:get("mood"),true)
			end
		end)
	end
	
	oldHealth = myHuman.Health
	
	if myHuman.Health <= 0 then
		local Info = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)
		local Properties1 = {BackgroundTransparency = 1}
		local Properties2 = {ImageTransparency = 1}

		HealthFill.Size = UDim2.new(0, 0, 1, 0)

		for i,v in pairs(HealthGUI:GetDescendants()) do
			if v:IsA("Frame") then
				local Tween = TweenService:Create(v,Info,Properties1)

				Tween:Play()
			elseif v:IsA("ImageLabel") then
				local Tween = TweenService:Create(v,Info,Properties2)

				Tween:Play()
			end
		end
	else
		HealthFill.Size = UDim2.new((myHuman.Health / myHuman.MaxHealth) * 1, 0, 1, 0)
		
		if myHuman.Health < 45 then
			for i,v in pairs(HealthFrame:GetChildren()) do
				if v.Name == "Borders" then
					v.BackgroundColor3 = Color3.fromRGB(189, 63, 63)
				end
			end
			
			HealthBar.BorderColor3 = Color3.fromRGB(189, 63, 63)
			HealthFill.BackgroundColor3 = Color3.fromRGB(189, 63, 63)
			HealthShading.BackgroundColor3 = Color3.fromRGB(194, 87, 87)
			
			HealthFrame.ImageLabel.ImageColor3 = Color3.fromRGB(189, 63, 63)
		else
			for i,v in pairs(HealthFrame:GetChildren()) do
				if v.Name == "Borders" then
					v.BackgroundColor3 = Color3.fromRGB(0, 189, 0)
				end
			end

			HealthBar.BorderColor3 = Color3.fromRGB(0, 189, 0)
			HealthFill.BackgroundColor3 = Color3.fromRGB(0, 189, 0)
			HealthShading.BackgroundColor3 = Color3.fromRGB(115, 189, 102)

			HealthFrame.ImageLabel.ImageColor3 = Color3.fromRGB(0, 189, 0)
		end
	end
end)