Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

how to find a bunch of parts that are close to a player and chose a random one?

i'm making a script that is supposed to find a bunch of parts near a player and chose one but i'm not sure of how to do it.

i've tried a bunch of techniques but i cant figure it out

here is a error message on the output BotScript:12: attempt to call method 'GetChildren' (a nil value)

and here is the script for it

		local char = game.Workspace:FindFirstChild(plr.Name)
		
		local mines = game.Workspace.Mines:FindFirstChild(plr.HidenStats.Working.Value).Folder
		
		local closeSpots = {}
		
		for i,v in pairs (mines:GetChildren())do
			if (char:FindFirstChild("HumanoidRootPart").Position-v.Position).Magnitude <= 20 then
				table.insert(closeSpots,i,v)
			end
		end
		local randomSpot = math.random(1,#closeSpots:GetChildren())
		local chosen = mines[randomSpot]

4 comments

0

use absolute value line 08

0

@greatneil no need, magnitude is always positive

0

try wait for child instead of find first child on line 3

0

this is just an example of a bigger script

2 answers

The error could be caused when there are no mines in reach. When there are no mines, the server tries to pick a random mine from nothing. To fix this, just do a check. Here is the script with the check. This should work.

local char = game.Workspace:FindFirstChild(plr.Name)

local mines = game.Workspace.Mines:FindFirstChild(plr.HidenStats.Working.Value).Folder

local closeSpots = {}

for i,v in pairs (mines:GetChildren())do
    if (char:FindFirstChild("HumanoidRootPart").Position-v.Position).Magnitude <= 20 then
        table.insert(closeSpots,i,v)
    end
end
if #closeSpots:GetChildren() >= 1 then
	local randomSpot = math.random(1,#closeSpots:GetChildren())
	local chosen = mines[randomSpot]
end

Your problem was that you were trying to :GetChildren() of an Array, "closeSpots". This doesn't make any sense because :GetChildren() RETURNS an array. Also :GetChildren() is a function of an Instance, and a table defined in a script is most definitely not an Instance.

Here is the wiki for the function: :GetChildren()

Also I removed the table.Insert(closeSpots,i,v) because I'm not sure if it's valid but regardless, the replacement I personally find to be far more elegant and general.

Here is your script.

local char = game.Workspace:FindFirstChild(plr.Name)

local mines = game.Workspace.Mines:FindFirstChild(plr.HidenStats.Working.Value).Folder

local closeSpots = {}

for i,v in pairs (mines:GetChildren()) do
    if (char:FindFirstChild("HumanoidRootPart").Position-v.Position).Magnitude <= 20 then
        closeSpots[i] = v -- changed
    end
end

if #closeSpots > 0 then  -- check incase their are no mines in the vicinity
	local randomSpot = math.random(1,#closeSpots) -- fix
	local chosen = mines[randomSpot]
end

Edit: Never mind I now know the table.Insert argument is valid, forgot xD, however i still prefer the method provided.

This discussion is preserved as it was. New answers, comments, and votes are closed.