Getting random position in part doesnt really work

function GameFunctions.getRandomPositionInPart(part)
	local x = part.Position.X + math.random(-part.Size.X/2,part.Size.X/2)
	local y = part.Position.Y + math.random(-part.Size.Y/2,part.Size.Y/2)
	local z = part.Position.Z + math.random(-part.Size.Z/2,part.Size.Z/2)

	return CFrame.new(x, y, z)
end

this function does work, it does get random position in a part but if i want to get the position in a part and if i want it to be precise (for example in a little part like 1,1,2 ) it cant get a random number between 0 - 1. it only gets either 0 or 1. any ways i can make it also work on smaller objects?

math.random() doesn’t factor in decimals, meaning math.random(0,1) will result in either 0 or 1. You can change this by multiplying the values within the parameters, then dividing them once you’re complete, creating decimal points.

You can see this with math.random(0, 1 * 100)/100 which will fetch you a 2-point decimal value, still within the accepted parameters of your 0,1 range.

For your example, you should be able to do the following.

math.random((-part.Size.Z/2)*100,(part.Size.Z/2)*100)/100

That should hopefully work.

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.