Trying to destroy a part when it comes within a certain radius of a player

Heres the script, I’m struggling to make the magnitude. Radius as in a 3 dimensional direction of a player. Similar to a circle being around the player. I want to make it so if it’s in that circle, it’ll get destroyed.

this is a image of what I want to acheive.



	local function findTarget()
	local agroDistance = Vector3.new(20,1,20)
	local hitDistance = 20
	local target = nil
	for _, v in pairs(game.Workspace:GetChildren()) do
		if v:IsA("Part") then
			local npc = game.Players:FindFirstChild("AE_LuaDev").Character
			if v ~= npc then
				if (Torso.Position - v.Position).Magnitude < agroDistance then
					
				
					agroDistance = (Torso.Position - v.Position).Magnitude
				

					if (Torso.Position - v.Position).Magnitude <= hitDistance then
						v:Destroy()
							end
	
						end
						
						end
					end
						
					
					end
				end
while wait() do
	findTarget()
end

Hi! Try this code:

local player = game.Players.LocalPlayer

-- Function to get all parts within 5 units of a player
local function getPartsInRange(player)
  local parts = {}
  for _, part in pairs(game.Workspace:GetChildren()) do
    if part:IsA("BasePart") then
      local distance = (part.Position - player.Character.Head.Position).magnitude
      if distance <= 5 then
        table.insert(parts, part)
      end
    end
  end
  return parts
end

-- Function to destroy all parts within 5 units of a player
local function destroyParts(player)
  local parts = getPartsInRange(player)
  for _, part in pairs(parts) do
    part:Destroy()
  end
end

-- Main loop
while true do
  destroyParts(player)
  wait(1)
end

The code in this code was AI generated from: https://chat.openai.com/chat

1 Like

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