Loop player in radius

Hi guys, i try to loop players in radius of my boss :slight_smile: i have try to make this script but it’s not work:

local heal = script.Parent.Ennemy
range = 100

Players = game:GetService("Players")
for i, player in pairs(Players:GetPlayers()) do
	while wait(1) do
	if heal.Health <= 2550 then
		local fox = script.Parent.PrimaryPart.Position
		local dist = (fox - player.Character.PrimaryPart.Position).magnitude
		if dist <= range then
			print(player.Name)
		end
	end
	end
end

Someone can help me please ?

1 Like

Encompass the loop inside a spawn function so each player receives a looped thread of their own. Either that or run the for loop for every cycle of the while loop.

2 Likes

Change the order of the loops

local heal = script.Parent.Ennemy
range = 100

Players = game:GetService("Players")
while true do
    for i, player in pairs(Players:GetPlayers()) do
        if heal.Health <= 2550 then
            local fox = script.Parent.PrimaryPart.Position
            local dist = (fox - player.Character.PrimaryPart.Position).magnitude
            if dist <= range then
                print(player.Name)
            end
        end
    end
    wait(1)
end

Thank you :wink: it’s working perfectly

1 Like