Why is this script freezing

so I have this script:

local cam = game.Workspace.CurrentCamera
local LockedGuy = game.ReplicatedFirst.LockedOne.Value
local UIS = game:GetService("UserInputService")
local toggle = false
local TS = game:GetService("TweenService")
local plr = game.Players.LocalPlayer
local char = game.Workspace:WaitForChild(plr.Name)
local hum = char.Humanoid
local OrigCframe
local Is2D = false


local function getOffsetInBetweenTwoParts(part1Pos,part2Pos)
	local magnitude = (part1Pos - part2Pos).Magnitude
	local Cframe = CFrame.new(part1Pos,part2Pos)* CFrame.new(Vector3.new(0,0,-magnitude/2)) * CFrame.Angles(math.rad(0),math.rad(90),math.rad(0)) *CFrame.new(Vector3.new(0,0,-magnitude/2))
	local p = Instance.new("Part")
	p.CFrame = Cframe
	p.Anchored = true
	p.Parent = workspace
	return Cframe
end
local function LockCamera2D(Target)
	while Is2D == true do
		local tweenInfo = TweenInfo.new(0.1,Enum.EasingStyle.Quad,Enum.EasingDirection.In)
		local goal = {
			CFrame = getOffsetInBetweenTwoParts(char.HumanoidRootPart.Position,Target.HumanoidRootPart.Position)
		}
		local tween = TS:Create(cam,tweenInfo,goal)
		tween:Play()
		tween.Completed:Wait()
	end
end

local function LockCamera3D(Target)
	while toggle == true do
		if Is2D == false then
			local tweenInfo = TweenInfo.new(0.1,Enum.EasingStyle.Quad,Enum.EasingDirection.In)
			local goal = {CFrame = CFrame.new(char.HumanoidRootPart.Position, head.Position) * CFrame.new(5,3,10)  }
			local tween = TS:Create(cam,tweenInfo,goal)
			tween:Play()
			tween.Completed:Wait()
		end
	end
end

UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.LeftControl then
		wait(0.1)
		LockedGuy = game.ReplicatedFirst.LockedOne.Value
		if LockedGuy then
			if toggle == false then
				toggle = true
				head = LockedGuy.Head
				OrigCframe = cam.CFrame:ToObjectSpace()
				cam.CameraType = Enum.CameraType.Scriptable
				cam.CameraSubject = hum
				LockCamera3D(LockedGuy)
			end
		else
			toggle = false
			Is2D = false
			cam.CameraType = Enum.CameraType.Custom
		end
	elseif Input.KeyCode == Enum.KeyCode.LeftAlt then
		if toggle == true then
			if Is2D == false then
				LockedGuy = game.ReplicatedFirst.LockedOne.Value
				Is2D = true
				wait(0.1)
				LockCamera2D(LockedGuy)
			else
				Is2D = false
				if toggle == true then
					wait(0.1)
					LockCamera3D(LockedGuy)
				end
			end
		end
	end
end)

and it freezes when I press Alt
the output says:
23:49:47.570 - Script timeout: exhausted allowed execution time

23:49:47.571 - Stack Begin

23:49:47.572 - Script ‘Players.Temeraire149.PlayerScripts.LockCamera’, Line 58

23:49:47.572 - Stack End

what i want to do is to prevent that happening.

thanks,
Tem.

Looks like you have a possible infinity loop in the function

LockCamera3D()

consider add a wait() in it

if Is2D == false then
-- existing code here
else
wait() -- to prevent time out.
end

You also have an infinite loop in LockCamera2D(). You need to add a wait() of some sort in there too. I missed the tween.Completed:Wait()

LockCamera2D()

doesn’t have an infinity loop it uses tween.Completed:Wait() at the end.

Ah, looking back at it, I see where it would cause an infinite loop. Yeah, you’re completely right.