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

Get the positions WITHIN a part?

The title may be a bit confusing, but let's take the default Baseplate for example. Let's say the Baseplate's size is (512, 20, 512). The position is (0,0,0). When I try to position a part randomly onto that baseplate, I find it hard to think of how to do it. Say if I do position a part to 0,0,0; it would only be in the MIDDLE of it. How do I find the amount of studs within? I've accomplished this by taking the locations of the X and Z of the ends, but I just want to do all of that in a script. Confused? Comment

Script:


function snow()
	while wait() do
-- got here try 1
	a = game.Lighting.Part:clone()
	print('cloned')
	for i,v in pairs(game.Workspace:GetChildren()) do
		if v.Name == "Baseplate" then
			a.Position = Vector3.new(math.random(-257,257), 200, math.random(-254,254))
		end
	end
	game.Workspace["Snow Script"].Script:clone().Parent = a
	a.Parent = Workspace
	a.Script.Disabled = false
	print('set')
	end
end

snow()

2 answers

✓ Accepted answer

You can use the concept of object space.

Normally, positions are relative to some absolute reference -- the origin (0,0,0).

That choice is arbitrary; we can recompute positions relative to any object.

local inside = part.Size
	* Vector3.new( math.random() - 0.5, math.random() - 0.5, math.random() - 0.5);
-- A random point on the interior of part.Size

local world = part.CFrame:pointToWorldSpace( inside )
-- world is a Position that you can use, that will be somewhere inside `part`



You shouldn't be using the loop that you are on line 6.

That would be much better phrased like this:

a.Position = Vector3.new(math.random(-257,257), 200, math.random(-254,254))

(you weren't even using v at all in the loop! If you were, instead just use workspace.Baseplate)

If the size is 512 by 512 and the position is 0, then positioning the snow between 512/2 and -(512/2) should give us a place on the baseplate.

Since the base is positioned at 0,0,0, half of it will be on the positive side of the coordinate plain and half of it will be on the negative side of the coordinate plain. Therefore, the brick must be somewhere between 256 and -256 on both the x and z axis. (256 is half of 512.) I have the x, y, and z separated for clarity.

part.Position = Vector3.new(
--x axis; random position between 256 and -256
math.random(-256, 256),
--y axis; always fall at the same height
200,
--z axis; random position between 256 and -256
math.random(-256, 256)
)

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