Help with finding dummies in certain range

how I would I check if there were any humanoids in the max range distance?? here is script

local plr = game.Players.LocalPlayer
local char = plr.Character
local mouse = plr:GetMouse()
local tweenService = game:GetService("TweenService")


mouse.Button1Down:Connect(function()
	local hrp = char.HumanoidRootPart
	local maxrange = 50

how do I check if there are any humanoids in the max range?
could I use I, v in pairs() and then get the humanoid and check if the magnitude between the hip and the dummies hip is under 50 would that work?

This would work but it’s not very performant in the long run

You could use a module such as ZonePlus to create an efficient hitbox

But this is just a suggestion

Yes, that’s exactly how you could find them. If you have a folder that contains all of these dummies (or use CollectionService), you could use the method the object has. :GetChildren() returns an array of instances that are parented directly under an object. You can then iterate through this array and subtract the two positional vectors (dummy’s position vector and hrp’s position vector) and get the magnitude from that:

local dummiesInRange = {}

for i, dummy in folder:GetChildren() do
    if (dummy:GetPivot().Position - hrp.Position).Magnitude <= 50 then
        table.insert(dummiesInRange, dummy)
    end
end

print(dummiesInRange) -- All of the dummies that are within 50 studs of the hrp

I would recommend using CollectionService as this allows you to specify which dummies you want to iterate over while allowing them to be located anywhere inside of the workspace – and not locked to a specific folder or other type of instance.

1 Like

I used the I, v in pairs and came up with this

is this an efficient way of doing this script or is there a better way to do it??

local plr = game.Players.LocalPlayer
local char = plr.Character
local mouse = plr:GetMouse()
local tweenService = game:GetService("TweenService")


mouse.Button1Down:Connect(function()
	local hrp = char.HumanoidRootPart
	local maxrange = 50
	
	
	for i, v in pairs(game.Workspace.Noobs:GetChildren()) do
		if v:FindFirstChild("Humanoid") then
			if v.Name == plr.Name then
				return 
			else
				local hrp = v:FindFirstChild("HumanoidRootPart")
			local sphere = game.ReplicatedStorage.Sphere:Clone()
			sphere.Parent = workspace
			local distance = (hrp.Position - char:FindFirstChild("HumanoidRootPart").Position).Magnitude
			if distance <= maxrange then
			

				local tweenInfo = TweenInfo.new(
					0.4, --seconds to complete
					Enum.EasingStyle.Quint, --easing style
					Enum.EasingDirection.Out, --easing direction
					0, --repeat count
					false, --reverses?
					0 --delay before starting
				)

				local properties = {
					Position = v:FindFirstChild("HumanoidRootPart").Position
				}
				wait()
                 sphere.Position = char:FindFirstChild("HumanoidRootPart").Position
				local tween = tweenService:Create(sphere, tweenInfo, properties)

				tween:Play()
				
				game.Debris:AddItem(sphere,0.4)
			end
			end
		end
	end
end)

There are some optimizations it could use but you shouldn’t see much performance issues since this isn’t a function tied to a loop or a RunService event. If I were to rewrite what you have, it would be like this:

mouse.Button1Down:Connect(function()
	local charhrp = char.HumanoidRootPart
	local maxrange = 50


	for i, v in pairs(game.Workspace.Noobs:GetChildren()) do
		if v.PrimaryPart and v.Name ~= plr.Name then
			local hrp = v.PrimaryPart
			local sphere = game.ReplicatedStorage.Sphere:Clone()
			sphere.Parent = workspace
			local distance = (hrp.Position - charhrp.Position).Magnitude
			if distance <= maxrange then


				local tweenInfo = TweenInfo.new(
					0.4, --seconds to complete
					Enum.EasingStyle.Quint, --easing style
					Enum.EasingDirection.Out, --easing direction
					0, --repeat count
					false, --reverses?
					0 --delay before starting
				)

				local properties = {
					Position = hrp.Position
				}
				wait()
				sphere.Position = charhrp.Position
				local tween = tweenService:Create(sphere, tweenInfo, properties)

				tween:Play()

				game.Debris:AddItem(sphere,0.4)
			end
		end
	end
end)
1 Like

ok thank u. u have helped me alot rlly appreciate it bro

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