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?
1 | function cloneCrop 1 () |
2 | crop 1 :clone().Parent = dirt |
3 | crop 1. Position = Vector 3. 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 |
4 | end |
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.
1 | function cloneCrop 1 () |
2 | local c = crop 1 :clone(); |
3 | local extents = dirt.Size.X/ 2 ; --Extents of dirt |
4 | local x = math.random(-extents,extents); --Randomized x-offset |
5 | c.Position = dirt.Position + Vector 3. new(x, 1.8 , 0 ); --Place it |
6 | c.Parent = dirt; |
7 | end |