How do i position a zone thats using math.random

The Title might be confusing but what i mean is that, im making an attack where bones rise from the ground randomly and im using math.random, it works but i want to know how do i move the zone where the bones are rising from

	for i = 1, 200 do
				local RandomNumber = math.random(2,82)
				local RandomNumber2 = math.random(2,82)
				local randombonesclone = game.ServerStorage.RandomBones:Clone()
				randombonesclone.Parent = game.Workspace
				randombonesclone.Position = Vector3.new(RandomNumber,-42,RandomNumber2)
				wait(0.01)
				for e = 1,10,1 do
					randombonesclone.Position = randombonesclone.Position + Vector3.new(0,1,0)
					wait(0.001)
				end
			end

if you still dont get it then watch this vid

they spawn on that side and i want them to spawn on the snow side

i might get a bit confused cuz im not that good with this math thingy
but help will be appreciated :smile:

2 Likes

I would suggest using a part as the center of where the bones spawn, and then spawn zombies randomly by using the part’s position, instead of a position in the world. This way, you can move the part around and the bones will keep spawning around the part :slight_smile:

1 Like

The following function will give you a random point within a part:

local function getRandomInPart(part)
	local xS = part.Size.X / 2
	local zS = part.Size.Z / 2
	local x = part.Position.X
	local z = part.Position.Z
	local posX = math.random(x - xS, x + xS)
	local posZ = math.random(z - zS, z + zS)
	return Vector3.new(posX, part.Position.Y, posZ)
end

So when the attack is fired, create a part which covers the area of attack, then call the above function ‘n’ times to return you a new random location for each bone, for example:

local newpart = Instance.new("Part")
newpart.Size = Vector3.new(10, 1, 10)
newpart.Position = getRandomInPart(newpart)
newpart.Parent = workspace

Based of what you mentioned im guessing you want it to rise around the mouse?

local origin = mouse.Hit.Position
local radius = 82
for i = 1, 200 do
	local x = math.random(-radius,radius)
	local z = math.random(-radius,radius)
	local randombonesclone = game.ServerStorage.RandomBones:Clone()
	randombonesclone.Parent = game.Workspace
	randombonesclone.Position = origin + Vector3.new(x,-42,z)
	wait(0.01)
	for e = 1,10,1 do
		randombonesclone.Position = randombonesclone.Position + Vector3.new(0,1,0)
		wait(0.001)
	end
end

hello! this is exactly what i was looking for, but i think i messed up something, im trying to understand it before applying it to my script but the bones are spawning in a straight line, this is what ive done so far

local part = script.Parent


local function getRandomInPart(part)
	local xS = part.Size.X / 1.32
	local zS = part.Size.Z / 1.32
	local x = part.Position.X / 15.367
	local z = part.Position.Z / -201.647
	local posX = math.random(x - xS, x + xS)
	local posZ = math.random(z - zS, z + zS)
	return Vector3.new(posX, part.Position.Y, posZ)
end

for i = 1,100,1 do
local newpart = game.ServerStorage.RandomBones:Clone()
newpart.Position = getRandomInPart(newpart)
	newpart.Parent = workspace
	wait(1)
end


ill keep trying fix it myself but i wanted to ask you first to make sure i didnt forget something

oh and yes the script is inside this part

	local xS = part.Size.X / 2 -- this gives us 1/2 of the part size on X axis
	local zS = part.Size.Z / 2 -- this gives us 1/2 of the part size on Z axis
	local x = part.Position.X -- gives us the centre of the part on X
	local z = part.Position.Z -- gives us the centre of the part on Y
	local posX = math.random(x - xS, x + xS) -- generates a position which is from the centre of the part + or - 1/2 of the random part size
	local posZ = math.random(z - zS, z + zS)

The changed numbers you have added are messing with the calculations. How are you specifying the position of the part? Is it based on a Mouse.Hit for the attack?

If yes, then you need to set newpart Position to the Mouse.Hit location and then do your loop to create the “bones” by calling the function.

The issue here is that you are only giving it a certain range to possibly have a chance to spawn in.


When create your RandomNumber which is your x-axis in the world and RandomNumber2 which is your z-axis in the world, you’re only letting it be spawned in that area of 2-82 on the x-axis and 2-82 on the z-axis.

To fix this simply find the x and z position of where you want your bones to spawn. An easy way of doing this is just creating a part and moving it to the general area you want your bones to spawn and taking its x and z positions and replacing the math.random with values similar to it in your desired range.