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

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

Asked by
Yeevivor4 155
9 years ago

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

Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

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
        )
    )
Ad
Log in to vote
1
Answered by 9 years ago
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

Answer this question