How to move a part with a random position selection?

I have a problem With math.random() to move a part

My script:

local Part = game.Workspace.Part

local Positions = {
	Vector3.new(-37.24, 0.5, -3.101);
	Vector3.new(41.328, 0.5, 81.018);
	Vector3.new(-95.248, 0.5, 90.467);
}

Part.Position = math.random(#Positions)

Please Help me!

Your code should be

local Part = game.Workspace.Part

local Positions = {
	Vector3.new(-37.24, 0.5, -3.101);
	Vector3.new(41.328, 0.5, 81.018);
	Vector3.new(-95.248, 0.5, 90.467);
}

Part.Position = Positions[math.random(1,#Positions]
1 Like

Only one thing you have to close the column The code Complete is this:

local Part = game.Workspace.Part

local Positions = {
	Vector3.new(-37.24, 0.5, -3.101);
	Vector3.new(41.328, 0.5, 81.018);
	Vector3.new(-95.248, 0.5, 90.467);
}

Part.Position = Positions[math.random(1,#Positions)]

Thank you so much! :grinning:

local Part = workspace:WaitForChild("Part")

local Positions = {
	Vector3.new(-37.24, 0.5, -3.101);
	Vector3.new(41.328, 0.5, 81.018);
	Vector3.new(-95.248, 0.5, 90.467);
}

Part.Position = Positions[math.random(1, #Positions)]

I’d recommend adding a WaitForChild() to make sure that the part has loaded before the script executes.

1 Like