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

Function Doesnt Clone Part And Position Randomly?

Asked by 2 years ago

what this script is made to do is it there's a function that sets a random position for a part and gets the part name, then it clones the part, then starts setting the clone value / variable to a clone of the previous clone and sets the position to random it repeats that 20 times before stopping, but it doesn't work, for some reason it only changes the position for the current part (not a clone) if anyone has some information on how to fix it or (even better) an actual working script, i would greatly appreciate it

01local function random(position, who)
02    for _, child in ipairs(script.Parent.poggy:GetChildren()) do
03        if child.Name == who then
04            child.Position = position
05        end
06    end
07end
08 
09local area = 354
10local baseplate_position=Vector3.new(177.753, 861.364, -402.773)
11 
12while true do
13    local clone = script.Parent.poggy.Smiler:Clone()
14    random(Vector3.new(math.random(-area/2,area/2),1,math.random(-area/2,area/2))+baseplate_position, clone.Name)
15    for i = 1, 20 do
16        clone = clone:Clone()
17        random(Vector3.new(math.random(-area/2,area/2),1,math.random(-area/2,area/2))+baseplate_position, clone.Name)
18        end
19    break
20end

1 answer

Log in to vote
1
Answered by 2 years ago

It's probably because either there are many parts with the same name or the parent of the clones is nil. You don't need to get the name and use the function, just clone script.Parent.poggy.Smiler 20 times using for i = 1, 20 do. Also you don't need to put that loop in a while loop.

01local function random(position, who)
02    for _, child in ipairs(script.Parent.poggy:GetChildren()) do
03        if child.Name == who then
04            child.Position = position
05        end
06    end
07end
08 
09local area = 354
10local baseplate_position=Vector3.new(177.753, 861.364, -402.773)
11 
12for i = 1, 20 do
13    local clone = script.Parent.poggy.Smiler:Clone()
14    clone.Parent = script.Parent.poggy
15 
16    math.randomseed(i) -- this is optional if you want to make it "more random"
17    clone.Position = Vector3.new(math.random(-area/2,area/2),1,math.random(-area/2,area/2))+baseplate_position
18end
Ad

Answer this question