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

How do I make my cloned parts move to random positions from a brick?

Hi, thank you for reading. What I'm trying to do here is that when my parts are spawned, they will move to a Part. The trick is to move it to random positions away from the part. I want to move it only a couple studs. What I tried to do is use ** math.random** at line 19 but it just keeps going far away and the parts just keep spawning at the same spots. How do I do this?

local loot = game.ServerStorage.Loot:GetChildren()
local names = {"Wood", "Foil", "Nail", "File", "DuctTape", "Razor", "MetalSheet" }

while wait(.1) do
    for n = 1, #names do
	    local currentName = names[n]
  	  if not game.Workspace:FindFirstChild(currentName) then
      	  for i = 1, #loot do
        	    if loot[i] and loot[i].Name == currentName then
           	     local lootItemToAdd = loot[i]:Clone()
              	  lootItemToAdd.Parent = game.Workspace
             	   if workspace.Part then
               	     local part = workspace.Part3
                 	   if lootItemToAdd:IsA("Part") then
                    	    print "Moving a part"
                     	   lootItemToAdd.CFrame = CFrame.new(part.Position)
                   	 elseif lootItemToAdd:IsA("Model") then
                    	    print "Moving a model"
                   	     lootItemToAdd:MoveTo(part.Position * math.random(1.2,1.5),0,part.Position * 	math.random(1.2,5))
      	              end
      	          end
   	         end
         end
        end
    end
end

2 answers

✓ Accepted answer

Try to add randomly generated numbers, since we're dealing with a Vector3 property.


#EDIT:

I forgot about the Vector3 constructor. Thank you, FutureWebsiteOwner.

lootItemToAdd:MoveTo(part.Position + Vector3.new(math.random(12,15)/10, 0, math.random(12,50)/10))

Explosion view (for visualization purposes)

lootItemToAdd:MoveTo
	(
	part.Position + Vector3.new
		(
			math.random(12,15)/10,
			0,
			math.random(12,50)/10
		)
	)
lootItemToAdd:MoveTo(part.Position + Vector3.new(math.random(12,15)/10, 0, math.random(12,15)/10))
--@Redbullusa, you forgot Vector3 :P
--you can only use math.random for integers so instead of 1.2, use 12/10 and 1.5, use 15/10

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