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

Messed up random placement script, random X coordinate works, Z - not so well, any guesses? [Solved]

Hello, I'm currently working in a little RNG-based system to place objects within a specific range (in this case, as long as it is in bounds of a specific part), this means that the X and Z are going to be random, but the Y is going to be constant.

My expected result is for parts to be scattered randomly around the selected field, but I messed something up, and for some reason, random X works, and while Z kind of does, the differences in Z coordinates are very minuscule.

Result of my current script (image link): https://cdn.discordapp.com/attachments/482924987288780801/765527140174528542/unknown.png

Current script (generation happens in a client-sided ModuleScript):

generator.generate = function(Slide, Amount)
	local SlideSpawn = Slide:FindFirstChild("Spawn");
	local SlideEdgeX = Vector2.new((SlideSpawn.Position - Vector3.new(SlideSpawn.Size.X/2, 0, 0)).X, (SlideSpawn.Position + Vector3.new(SlideSpawn.Size.X/2, 0, 0)).X);
	local SlideEdgeY = Vector2.new((SlideSpawn.Position - Vector3.new(0, 0, SlideSpawn.Size.Y/2)).Z, (SlideSpawn.Position + Vector3.new(0, 0, SlideSpawn.Size.Y/2)).Z);
	for i = 0, Amount do
		local rng = Random.new()
		local ToPaste = Instance.new("Part");
		ToPaste.Size = Vector3.new(1, 1, 1);
		ToPaste.Anchored = true;
		ToPaste.Parent = Slide;
		local RanX = rng:NextNumber(SlideEdgeX.X, SlideEdgeX.Y);
		local RanZ = rng:NextNumber(SlideEdgeY.X, SlideEdgeY.Y);
		print(SlideEdgeY.X, SlideEdgeY.Y);
		ToPaste.Position = Vector3.new(RanX, SlideSpawn.Position.Y, RanZ);
	end
end

4 comments

1

Since you're using Vector2 you need to consider that, with modern ratios (16:9), X > Y. This means that Y has a smaller range to work with, which results in smaller changes. If you want larger changes you need X for both the X and Z 3D coordinates.

1

Could you by any chance drop a source or article on whatever it is you're talking about? I simply do not understand anything.

1

It's related to pixels on a screen. There are less pixels vertically than there are horizontally. A 1920x1080 screen is a perfect example of this, and because the range is smaller, Vector2 has to comply. So you either need to exaggerate Y or use X for Z

1

I'm sorry, but how are pixels and aspect ratios of monitors related to Vector3s and 2s which are in use in physical parts with physical sizes?

1 answer

Managed to correct the issue myself, not sure what exactly I did, but it worked. The two modified lines:

	local SlideEdgeX = Vector2.new((SlideSpawn.Position - Vector3.new(SlideFromCenter, 0, 0)).X, (SlideSpawn.Position + Vector3.new(SlideFromCenter, 0, 0)).X);
	local SlideEdgeY = Vector2.new((SlideSpawn.Position - Vector3.new(0, 0, SlideFromCenter)).Z, (SlideSpawn.Position + Vector3.new(0, 0, SlideFromCenter)).Z);

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