Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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]
0
use absolute value line 08 greatneil80 2647 — 5y
0
@greatneil no need, magnitude is always positive Aniline_Purple 266 — 5y
0
try wait for child instead of find first child on line 3 RetroGalacticGamer 331 — 5y
0
this is just an example of a bigger script Nooberton_1 9 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

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
Ad
Log in to vote
0
Answered by
Aznarog 54
5 years ago
Edited 5 years ago

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.

Answer this question