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

How to ensure a part clone is not in a custom set radius using magnitude?

I am trying to spawn parts with a magnitude of 50 from a part, the center. How do I go about doing this??

main = workspace.Enemy
tower = workspace.Tower
spawnradius = 50
while true do
	part = main:Clone()
	part.BrickColor = BrickColor.Random()
	mag = (part.Position - tower.Position).magnitude
	if mag <50 then
		-- I have no idea what to do.
	end
end

I've thought about trying to understand how Roblox calculates magnitude but it has to do with something on absolute values. But being a 13-year-old, I have no idea how to find that. Any help would be appreciated. Solutions don't have to only rely on magnitudes.

2 comments

0

This might prove useful in just understanding magnitude: https://forum.scriptinghelpers.org/topic/763/how-magnitude-works-in-layman-s-terms

0

Thanks but it's shows errors. Lol.

1 answer

✓ Accepted answer

You could use CFrames to accomplish this since CFrame is in object (local) space, so if you're referencing a point with the position (25,50,0) you're actually referencing the point relative to the corresponding axes; 25 studs in the (object's) X axis, 50 studs in the (object's) Y axis and 0 studs in the (object's) Z axis.

math.randomseed(tick())
part.CFrame = CFrame.new(0,0,0) * CFrame.Angles(math.rad(math.random(0,360) - 180),math.rad(math.random(0,360) - 180),math.rad(math.random(0,360) - 180)) -- set random rotation
part.CFrame = part.CFrame * CFrame.new(math.random(50,75),0,0) -- move 50-75 studs "forward"
part.CFrame = CFrame.new(part.CFrame.Position) * CFrame.Angles(0,0,0) -- reset rotation

3 comments

1

Hmmmm it works but not the results I am trying to achieve. I want parts to spawn outside a circle. In the middle of the circle is a part. So I changed line 3 to *CFrame.new(math.random(50,75),0,math.random(50,75)) But I am getting a half circle instead of a full circle. I need a way to store the negative values too.

0

It should work as you described now.

1

Somewhat works! I'll just try to tweak to fix the issues. Thanks!

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