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

Part moves towards one part in a table and not other table parts?

I'm trying to make this script move each MoverPart that's spawned use the BodyPosition object to go towards a random object from a table. Whenever I test it in Studio, each part that's cloned only goes towards one object, not a random object.

The MoverPart is cloned using a separate script.

local MoverPart = script.Parent

local CurrentMap = workspace:WaitForChild("CurrentMap")
local CurrentMapGeo = CurrentMap:FindFirstChild("Geometry")
local Content = CurrentMapGeo:GetChildren()

local BodyObject = Instance.new("BodyPosition")
BodyObject.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
BodyObject.Parent = MoverPart

ObjectTargets = {}

table.insert(ObjectTargets, Content)

BodyObject.Position = Vector3.new(0,  ObjectTargets[math.random].Position,  0)

The rest of the script is not included

1 answer

✓ Accepted answer

First thing, GetChildren returns an array, therefore because of line 13, 'ObjectTargets' is an array with a single value (which is the 'Content' array). What you want is to pick a random object from the 'Content' array. So lines 11 through 13 are unnecessary.

Next, on line 15 you are trying to set a Vector3 with the parameters ( number, Vector3, number ) which is not valid. If you want the BodyObject position to be the same as the part, you'll want to set it directly to the position of the target you find.

Also, the function math.random gives you a value between 0 and 1 with its default parameters, but you want a random number between 1 and the number of values in the 'Content' array.

Solution:

local MoverPart = script.Parent

local CurrentMap = workspace:WaitForChild("CurrentMap")
local CurrentMapGeo = CurrentMap:FindFirstChild("Geometry")
local Content = CurrentMapGeo:GetChildren()

local BodyObject = Instance.new("BodyPosition")
BodyObject.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
BodyObject.Parent = MoverPart

local key = math.random(1, #Content)
BodyObject.Position = Content[key].Position

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