Hello! I've been working on a little side project recently and have run into a recurring problem; infinite loops. Here is an example of one of these problems:
x = true
wait(4)
player = game.Workspace:FindFirstChild("Juolite")
while x do
if (script.Parent.Position - player.HumanoidRootPart.Position).Magnitude <= 40 then
script.Parent.PointTwo.Transparency = 0
else
script.Parent.PointTwo.Transparency = 1
end
wait(0.2)
end
In the code above, I've got a little part (PointTwo) that appears when a player is within a certain radius of the script's Parent part (40 studs). I've just been learning how .Magnitude works. The code runs fine. The problem is, I'd like to avoid using an infinite loop here, and in future scripts too. How should I go about doing this?

2 comments
w h y t h o u g h
Because infinite loops seem like they would lag up the game, checking every 0.2 seconds instead of firing upon something happening. Could you imagine thousands of infinite loops running in parallel? The horror...