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 1 year 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

local function random(position, who)
    for _, child in ipairs(script.Parent.poggy:GetChildren()) do
        if child.Name == who then
            child.Position = position
        end
    end
end

local area = 354
local baseplate_position=Vector3.new(177.753, 861.364, -402.773)

while true do
    local clone = script.Parent.poggy.Smiler:Clone()
    random(Vector3.new(math.random(-area/2,area/2),1,math.random(-area/2,area/2))+baseplate_position, clone.Name)
    for i = 1, 20 do
        clone = clone:Clone()
        random(Vector3.new(math.random(-area/2,area/2),1,math.random(-area/2,area/2))+baseplate_position, clone.Name)
        end
    break
end

1 answer

Log in to vote
1
Answered by 1 year 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.

local function random(position, who)
    for _, child in ipairs(script.Parent.poggy:GetChildren()) do
        if child.Name == who then
            child.Position = position
        end
    end
end

local area = 354
local baseplate_position=Vector3.new(177.753, 861.364, -402.773)

for i = 1, 20 do
    local clone = script.Parent.poggy.Smiler:Clone()
    clone.Parent = script.Parent.poggy

    math.randomseed(i) -- this is optional if you want to make it "more random"
    clone.Position = Vector3.new(math.random(-area/2,area/2),1,math.random(-area/2,area/2))+baseplate_position
end
Ad

Answer this question