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

Cloning block on random position inside a parent block?

Asked by 7 years ago
Edited 7 years ago

I just want the cloned block to stay inside the bounds of the dirt block that it's parented to but it goes out of the bounds of it. How can I fix that?

function cloneCrop1()
    crop1:clone().Parent = dirt
    crop1.Position = Vector3.new(math.random(0,35),1.8,0) -- Not sure how to make it randomly clone only on the x-axis while staying inside the bounds of the dirt
end

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

First, you have to know the bounds of the dirt. Since you're only using math.random on the x-axist, you only need to know the x bounds for the dirt.

This will be dirt.Size.X/2, since each side of the dirt is half its size away from the origin.

Now you can simply use math.random with negative bounds, and positive bounds.

function cloneCrop1()
    local c = crop1:clone();
    local extents = dirt.Size.X/2; --Extents of dirt
    local x = math.random(-extents,extents); --Randomized x-offset
    c.Position = dirt.Position + Vector3.new(x,1.8,0); --Place it
    c.Parent = dirt;
end
0
Is this going to work for a ServerScript I added it and it just stopped working? I changed script.Parent to the dirt which I have before it all. YouSNICKER 131 — 7y
0
Lol I forgot to parent it, you're good. Goulstem 8144 — 7y
0
Hmm, still spawns outside for some reason.. Could I invite you to a TeamCreate and you can take a look at it? YouSNICKER 131 — 7y
0
Sure. Goulstem 8144 — 7y
View all comments (2 more)
0
Thanks for the help! YouSNICKER 131 — 7y
Ad

Answer this question