Resetting while flying causing my character to duplicate and bug and death sound palyed alot

My system is bugged as whenever I reset the characters players body parts that fall are duplicated causing my camera to teleport all over the place and the death sound is played multiple times
I’ve tried removing the body gyro are setting character to not have duplicates but none work any help will be appreciated.

Channel ->> https://www.youtube.com/c/coolcapidog
You can change the settings but you shouldn't change anything except settings.
]]--

-- Configuration
local HoverAnimID = "rbxassetid://92375094730481"
local FlyAnimID = "rbxassetid://108092256191052"
local WindSoundEnabled = true
local MaxSpeed = 400 -- Max flying speed
local MinSpeed = 100 -- Min flying speed
local MaxSpeedStat = 1000000000 -- Max speed stat required for max speed
local SpeedThreshold = 16000 -- Threshold speed value for incremental changes
local VelocityIncrement = 20 -- Amount to increase the velocity

-- Fly Speed Update Integration
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local FlySpeedUpdate = ReplicatedStorage:WaitForChild("FlySpeedUpdate")
local currentFlySpeed = MinSpeed -- Default fly speed

-- Listen for speed updates from the server
FlySpeedUpdate.OnClientEvent:Connect(function(newSpeed)
	currentFlySpeed = newSpeed
end)

-- Templates for BodyGyro and BodyVelocity
local BodyVelocityTemplate = script:WaitForChild("BodyVelocity")
local BodyGyroTemplate = script:WaitForChild("BodyGyro")

-- Character and Humanoid initialization
local Character = script.Parent
local Humanoid = Character:FindFirstChild("Humanoid") or Character:WaitForChild("Humanoid")

-- Function to ensure the HumanoidRootPart is found
local function getHumanoidRootPart()
	for i = 1, 10 do -- Try up to 10 times
		local part = Character:FindFirstChild("HumanoidRootPart")
		if part then
			return part
		end
		task.wait(0.1) -- Wait briefly before checking again
	end
	warn("HumanoidRootPart not found after several attempts.")
	return nil
end

local HumanoidRootPart = getHumanoidRootPart()
if not HumanoidRootPart then
	return -- Exit the script if HumanoidRootPart could not be found
end

-- Ensure BodyVelocity and BodyGyro are not already present
local function removeExistingBodyEffects()
	if HumanoidRootPart then
		local existingBodyVelocity = HumanoidRootPart:FindFirstChildOfClass("BodyVelocity")
		local existingBodyGyro = HumanoidRootPart:FindFirstChildOfClass("BodyGyro")
		if existingBodyVelocity then existingBodyVelocity:Destroy() end
		if existingBodyGyro then existingBodyGyro:Destroy() end
	end
end

-- Create animations and sounds
local Hover = Instance.new("Animation")
Hover.AnimationId = HoverAnimID
local Fly = Instance.new("Animation")
Fly.AnimationId = FlyAnimID
local Sound1 = Instance.new("Sound", HumanoidRootPart or Character)
Sound1.SoundId = "rbxassetid://3308152153"
Sound1.Name = "Sound1"
if not WindSoundEnabled then
	Sound1.Volume = 0
end

local HoverAnim = Humanoid.Animator:LoadAnimation(Hover)
HoverAnim.Priority = Enum.AnimationPriority.Action -- Ensures this animation has priority
local FlyAnim = Humanoid.Animator:LoadAnimation(Fly)
FlyAnim.Priority = Enum.AnimationPriority.Action -- Ensures this animation has priority
local Camera = game.Workspace.CurrentCamera

-- Function to stop sounds on reset (death)
local function stopSoundsOnReset()
	if Sound1 and Sound1.Playing then
		Sound1:Stop() -- Stop sound if it's playing
	end
end

-- Function to clean up flying-related objects (BodyVelocity and BodyGyro)
local function cleanUpFlyingObjects()
	if HumanoidRootPart then
		local bodyVelocity = HumanoidRootPart:FindFirstChildOfClass("BodyVelocity")
		local bodyGyro = HumanoidRootPart:FindFirstChildOfClass("BodyGyro")
		if bodyVelocity then
			bodyVelocity:Destroy()
		end
		if bodyGyro then
			bodyGyro:Destroy()
		end
	end
end

-- Function to stop flying and reset everything
local function stopFlyingAndReset()
	-- Stop animations and sounds
	FlyAnim:Stop()
	HoverAnim:Stop()
	Sound1:Stop()

	-- Reset humanoid state
	Humanoid:SetStateEnabled(Enum.HumanoidStateType.Running, true)
	Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, true)
	Humanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, true)
	Humanoid:SetStateEnabled(Enum.HumanoidStateType.Freefall, true)
	HumanoidRootPart.Running.Volume = 0.65
	Humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)

	-- Clean up BodyVelocity and BodyGyro objects
	cleanUpFlyingObjects()
end

-- Helper functions
local function getFlyDirection()
	if Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
		return Humanoid.MoveDirection
	end
	local direction = (Camera.CFrame * CFrame.new((CFrame.new(Camera.CFrame.p, Camera.CFrame.p + Vector3.new(Camera.CFrame.lookVector.x, 0, Camera.CFrame.lookVector.z)):VectorToObjectSpace(Humanoid.MoveDirection)))).p - Camera.CFrame.p
	return direction == Vector3.new() and Vector3.new() or direction.unit
end

local function getFlyingSpeed()
	return currentFlySpeed -- Use the dynamically updated speed
end

-- Variables
local Flymoving = script.Flymoving
local TweenService = game:GetService("TweenService")
local UIS = game:GetService("UserInputService")
local Flying = false
local lastTapTime = 0
local doubleTapInterval = 0.5 -- Time in seconds to detect double-tap

-- Check if the player has the flying ability equipped
local player = game.Players.LocalPlayer

local function hasFlyAbilityEquipped()
	local abilities = player:FindFirstChild("Abilities")
	if abilities then
		local flyAbility = abilities:FindFirstChild("Fly")
		local flyEquipped = abilities:FindFirstChild("Fly")
		if flyAbility and flyEquipped then
			return flyAbility.Value and flyEquipped.Value
		end
	end
	return false
end

-- Handle flying mechanics
local function ToggleFlying()
	if not hasFlyAbilityEquipped() then
		return
	end

	if not HumanoidRootPart then
		warn("HumanoidRootPart not found in character.")
		return
	end

	if not Flying then
		-- Start flying
		Flying = true
		FlyAnim:Play(0.1, 1, 1)
		HoverAnim:Stop()
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.Running, false)
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, false)
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, false)
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.Freefall, false)
		HumanoidRootPart.Running.Volume = 0

		-- Create and apply BodyGyro and BodyVelocity for flying
		removeExistingBodyEffects() -- Ensure no duplicates
		local bodyVelocity = BodyVelocityTemplate:Clone()
		local bodyGyro = BodyGyroTemplate:Clone()
		bodyVelocity.Parent = HumanoidRootPart
		bodyGyro.Parent = HumanoidRootPart

		-- Ensure BodyGyro is set up correctly
		bodyGyro.CFrame = Camera.CFrame
	else
		-- Stop flying
		Flying = false
		stopFlyingAndReset() -- Clean up everything and reset states
	end
end

-- Detect double-tap for spacebar
UIS.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == Enum.KeyCode.Space then
		local currentTime = tick()
		if currentTime - lastTapTime <= doubleTapInterval then
			ToggleFlying()
		end
		lastTapTime = currentTime
	end
end)

-- Detect double-tap for mobile touch
if UIS.TouchEnabled then
	pcall(function()
		local jumpButton = player:WaitForChild("PlayerGui"):WaitForChild("TouchGui"):WaitForChild("TouchControlFrame"):WaitForChild("JumpButton")
		if jumpButton then
			jumpButton.MouseButton1Down:Connect(function()
				local currentTime = tick()
				if currentTime - lastTapTime <= doubleTapInterval then
					ToggleFlying()
				end
				lastTapTime = currentTime
			end)
		end
	end)
end

-- Handle flying effects
game:GetService("RunService").RenderStepped:Connect(function()
	if Character == script.Parent then
		if Flying then
			Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
			if BodyGyroTemplate then
				local bodyGyro = HumanoidRootPart and HumanoidRootPart:FindFirstChildOfClass("BodyGyro")
				if bodyGyro then
					bodyGyro.CFrame = Camera.CFrame
				end
			end
			if getFlyDirection() == Vector3.new(0, 0, 0) then
				Flymoving.Value = false
			else
				Flymoving.Value = true
			end
			local speed = getFlyingSpeed()
			-- Apply the calculated speed to the BodyVelocity
			local bodyVelocity = HumanoidRootPart:FindFirstChildOfClass("BodyVelocity")
			if bodyVelocity then
				bodyVelocity.Velocity = getFlyDirection() * speed
			end

			-- Play hover animation if flying but not moving
			if not Flymoving.Value then
				HoverAnim:Play()
			else
				HoverAnim:Stop()
			end
		else
			-- Stop fly animation and hover animation when not flying
			FlyAnim:Stop()
			HoverAnim:Stop()
		end
	end
end)

Flymoving.Changed:Connect(function(value)
	if value then
		TweenService:Create(Camera, TweenInfo.new(0.5), {FieldOfView = 100}):Play()
		HoverAnim:Stop()
		Sound1:Play()
	else
		TweenService:Create(Camera, TweenInfo.new(0.5), {FieldOfView = 70}):Play()
		FlyAnim:Stop()
		Sound1:Stop()
		if Flying then
			HoverAnim:Play()
		end
	end
end)

-- Handle reset (death) and stop sounds
Humanoid.Died:Connect(function()
	stopFlyingAndReset() -- Cleanup and reset everything on death
end)