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