Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Cloning block on random position inside a parent block?

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

✓ Accepted answer

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

6 comments

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.

0

Lol I forgot to parent it, you're good.

0

Hmm, still spawns outside for some reason.. Could I invite you to a TeamCreate and you can take a look at it?

0

Sure.

0

Thanks for the help!

This discussion is preserved as it was. New answers, comments, and votes are closed.