Trying to move an object to another objects position from a table

So I am trying to recreate the ray casting example and I am trying to make the ball move around to the platforms randomly, but I have no clue how to make it so it gets the platforms position from the table and set it to the balls position.

local ball = script.Parent
local parts = {}

local bodyPos = Instance.new("BodyPosition")
bodyPos.Parent = ball

for _, part in pairs(workspace.Folder:GetChildren()) do
	if part:IsA("Part") then
		table.insert(parts, part.Position)
	end
end

print(parts)

for count = 15, 0, -1 do
	local move = #parts[math.random(#parts)]
	bodyPos.Position = parts.Position
end


I uploaded the example videe here

1 Like

instead:

for count = 15, 0, -1 do
local move = #parts[math.random(#parts)]
bodyPos.Position = parts.Position
end

try:

for count = 15, 0, -1 do
local move = #parts[math.random(#parts)]
bodyPos.Position = move.Position
end

I have tried that before, the orb stays still and doesn’t move. It also gives me an error: Workspace.Ball.Script:16: attempt to get length of a Vector3 value

I removed the # from parts[math.random(#parts)], It got me in the right direction, but this time I get a different error.

local ball = script.Parent
local parts = {}

local bodyPos = Instance.new("BodyPosition")
bodyPos.Parent = ball

for _, part in pairs(workspace.Folder:GetChildren()) do
	if part:IsA("Part") then
		table.insert(parts, part.Position)
	end
end

print(parts)

for count = 15, 0, -1 do
	local move = parts[math.random(#parts)]
	bodyPos.Position = move.Position
end

Position is not a valid member of Vector3 - Server - Script:17

on this part:

for _, part in pairs(workspace.Folder:GetChildren()) do
if part:IsA(“Part”) then
table.insert(parts, part.Position)
end
end

the part’s position is inserted into a table

do:
bodyPos.Position = move

Actually, you can change

local parts = {}

for _, part in pairs(workspace.Folder:GetChildren()) do
	if part:IsA("Part") then
		table.insert(parts, part.Position)
	end
end

print(parts)

for count = 15, 0, -1 do
	local move = parts[math.random(#parts)]
	bodyPos.Position = move.Position
end

to

local parts = workspace.Folder:GetChildren()

for count = 15, 0, -1 do
    local move = parts[math.random(#parts)]
    bodyPos.Position = move.Position
end
  • workspace.Folder should only have BaseParts
  • If you have other things in the folder, you should make a different folder in workspace and
    put other things in that folder.
1 Like

Neither of the solutions from you and Mac seem to work

Anyway, i fixed it. Hopefully it works.

local ball = script.Parent
local parts = {}

local bodyPos = Instance.new("BodyPosition", ball)

for _, part in pairs(workspace.Folder:GetChildren()) do
	if part:IsA("Part") and part ~= ball then
		table.insert(parts, part.Position)
	end
end

for count = #parts, 0, -1 do
	local move = parts[math.random(#parts)]
	bodyPos.Position = move + Vector3.new(0, 7, 0)
	
	task.wait(2)
end
local ball,parts  = script.Parent, {}
local TweenService = game:GetService("TweenService")

local bodyPos = Instance.new("BodyPosition")
bodyPos.Parent = ball

for _, part in pairs(workspace.Folder:GetChildren()) do
	if part:IsA("Part") then
		table.insert(parts, part.Position)
	end
end

local tweenDuration = 1
local tweenInfo = TweenInfo.new(tweenDuration)

for count = 15, 0, -1 do
	local move = parts[math.random(#parts)]
	local goal = {Position = move}
	local tween = TweenService:Create(bodyPos, tweenInfo, goal)
	tween:Play() wait(tweenDuration)
end

Doesn’t work, but it could be because BodyPosition is deprecated, even tho my friends script worked when he used BodyPosition. AlignPosition is the alternative so I might try that

Are you sure the ball is unanchored? If the ball is anchored it will not move.