Enemy wont shoot laser beams

I’m trying to make a laser beam for an enemy, instead it doesn’t print errors or shoot laser beams.

function joined2(player)
	local char = player.Character or player.CharacterAdded:Wait()
	local part1 = char.Head
	local part2 = script.Parent
	local magnitude = (part1.Position - part2.Position).magnitude
	while true do
		wait()
		if magnitude < 80 then
			while true do
				wait(20)
				local insan = Instance.new("Attachment",part1)
				script.Parent.Parent.Union.Beam.Attachment0 = insan
				local ez = workspace.Explodey:Clone()
				ez.Parent = workspace
				ez.Position = part1
			end
			wait()
		end
	end
end
game.Players.PlayerAdded:Connect(joined2)

Doesn’t print anything, and doesn’t do anything.

Try this:

	while wait() do
        local magnitude = (part1.Position - part2.Position).magnitude

Instead of:

local magnitude = (part1.Position - part2.Position).magnitude
	while true do
		wait()

I’ll try that, this will hopefully fix it.
edit: Thanks it worked!

How would I make the enemy know when the player is dead and look to find a new target?
For right now, it just shoots where the player once was and ignores new targets.

function findTarget()
	local mag = 80
	local target

	for v, plr in pairs(game.Players:GetPlayers()) do
		local char = plr.Character
		if char then
			local part1 = char:FindFirstChild("Head")
			local human = char:FindFirstChild("HumanoidRootPart")
			if part1 and human and human.Health > 0 then
				local part2 = script.Parent
				local magnitude = (part1.Position - part2.Position).magnitude
				if magnitude < mag then
					mag = magnitude
					target = part1
				end
			end
		end
	end
end

while wait() do
	local part1 = findTarget()
	if part1 then
		while part1 and part1.Parent do
			wait(20)
			local insan = Instance.new("Attachment",part1)
			script.Parent.Parent.Union.Beam.Attachment0 = insan
			local ez = workspace.Explodey:Clone()
			ez.Parent = workspace
			ez.Position = part1
		end
	end
end

Thanks alot, as per usual ill test this out.
NOTE: It does nothing, it print’s nothing. Is there anything I need to correct?