Flying script not working once you die due to infinite yield warning

Hi! I’ve made a fly script and everything is fine. That is until you reset, which for some reason makes it so that you can’t fly anymore. I’ll put the code here so you can understand. The code runs perfectly fine when you’re alive, but if you reset your character while flying, the script will still run on the function: startFly(), I’ve tried many ways of fixing it, even rearranging the entire script, but I always come across the same “infinite yield” warning which stops the script.

	local uis = game:GetService("UserInputService")
	local runser = game:GetService("RunService")
	local plr = game.Players.LocalPlayer
	local char = plr.Character or plr.CharacterAdded:Wait()
	local humroot = plr.Character:FindFirstChild("HumanoidRootPart")
	local CurCam = game.Workspace.CurrentCamera
	local anim
	local fly = false
	local v = 75
	local deboi = false
	local function startfly()
		script.fly:Play()*
	
	local alignori = Instance.new("AlignOrientation",humroot)
	alignori.Mode = Enum.OrientationAlignmentMode.OneAttachment
	alignori.Attachment0 = humroot:FindFirstChild("RootAttachment")
	alignori.Responsiveness = 50
	
	local alignpos = Instance.new("AlignPosition",humroot)
	alignpos.Mode = Enum.PositionAlignmentMode.OneAttachment
	alignpos.Attachment0 = humroot:FindFirstChild("RootAttachment")
	
	
	alignpos.MaxForce = 100000
	alignori.MaxTorque = 100000
	
	anim = plr.Character:WaitForChild("Humanoid"):LoadAnimation(script:FindFirstChild("Animation"))
	anim:Play()

	fly = true

	
	
	while fly do
		runser.RenderStepped:Wait()
		
		alignori.CFrame = CFrame.new(CurCam.CFrame.Position, humroot.Position)
		
		alignpos.Position = humroot.Position + ((humroot.Position - CurCam.CFrame.Position).unit * v)
		
		print(v)
	
	end
end

local function EndFly()
	
	if script.fly.Playing == true then
		script.fly:Stop()
	end
	
	if anim then
		anim:Stop()
	end
	
	local alor = plr.Character.HumanoidRootPart:WaitForChild("AlignOrientation")
	local alpos = plr.Character.HumanoidRootPart:WaitForChild("AlignPosition")
	
	if alor and alpos then
		alor:Destroy()
		alpos:Destroy()
	end
	
	fly = false
	v = 75
	CurCam.FieldOfView = 70
end



uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		if not fly then
			startfly()
		else
			EndFly()
		
		end
	end
end)

uis.InputBegan:Connect(function(input,gameprocess)
	if deboi then return end
	
	if input.KeyCode == Enum.KeyCode.T and fly and not gameprocess then
		
		deboi = true
		
		if v == 75 then
			game.ReplicatedStorage.SoundBarrier:FireServer()
			
			v = 350
			CurCam.FieldOfView = 120
			
			task.wait(15)
			v = 75
			CurCam.FieldOfView = 70
			
			task.wait(5)
			
			deboi = false
			 
		
			
		end
	end
end)

while wait() do
	if plr.Character:WaitForChild("Humanoid").Health == 0 then
		EndFly()
	end
end
game.Workspace.Baseplate.Touched:Connect(function(part)
	if part.Parent:FindFirstChild("Humanoid") and part.Parent.Name == plr.Name and fly then
		
		EndFly()
	end
	
end)

Use Humanoid.Died

I’ve tried it but it stops the whole script from working for some reason.

Found solution to problem, the script should have been in starter character instead of starter player. Why I didn’t think of this 3 hours ago? I don’t know.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.