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

Random Vector3 within a position?

So I'm trying to make a random position within a position but it does not seem to be very random The "if magnitude > 15 then" was an attempt to make it come back. No output errors...

force = script.Parent.BodyPosition
local spawn = game.Workspace.Spawn
local x2 = (spawn.Position.x)
local y2 = (spawn.Position.y)
local z2 = (spawn.Position.z)
print(Vector3.new(x2/3, y2, z2/3))
local GyroChance = math.random(1,5)
---print(GyroChance)
if GyroChance == 1 then
	if not script.Parent:FindFirstChild("BodyGyro") then
		----print("Gyro")
		local Gyro = Instance.new("BodyGyro")
		Gyro.Parent = script.Parent
	end
else
	if script.Parent:FindFirstChild("BodyGyro") then
		script.Parent:FindFirstChild("BodyGyro"):remove()
	end
end
---print(x2,y2,z2)
---local x = (math.random(z2,x2))
---local y = (math.random(y2,y2))
---local z = (math.random(x2,z2))
---print(x,y,z)
while true do
	wait(1)
	local magnitude = (script.Parent.Position - spawn.Position).magnitude
	local x2 = (spawn.Position.x)
	local y2 = (spawn.Position.y)
	local z2 = (spawn.Position.z)
	if magnitude > 15 then
		--print("To far!")
		force.position = Vector3.new(x2, y2, z2)
	else
		--print("Just rite")
	force.P = math.random(48000,55000)
	print(force.P)
	local pos1 = Vector3.new(x2,y2,z2)
	local pos2 = Vector3.new(x2/2, y2, z2/2)
	print(pos2)
	local randompos = (math.random(1,2))
	if randompos == 1 then
		force.position = Vector3.new(pos1)
	else
		force.position = Vector3.new(pos2)
	end
	print(force.position)
	end
end

1 answer

✓ Accepted answer

I highly suggest not using x y and z when you don't need to.

First, we can do a little to simplify / clean up the beginning of the script:

force = script.Parent.BodyPosition
local spawn = game.Workspace.Spawn
local pos = spawn.Position
print(pos.x / 3, pos.y, pos.z / 3) -- or `Vector3.new(1/3, 1, 1/3) * pos`

if script.Parent:FindFirstChild("BodyGyro") then
	script.Parent:FindFirstChild("BodyGyro"):Remove()
end
local GyroChance = math.random(1, 5)
if GyroChance == 1 then
	Instance.new("BodyGyro", script.Parent)
end

Now the loop can be slightly cleaner

while true do
	wait(1)
	local pos = spawn.Position
	local distance = (script.Parent.Position - pos).magnitude
	if distance > 15 then
		--print("To far!")
		force.position = pos
	else
		--print("Just rite")
		force.P = math.random(48000, 55000)

		local alt = pos * Vector3.new(1/2, 1, 1/2)
		print(alt)

		if math.random(1, 2) == 1 then
			force.position = pos
		else
			force.position = alt
		end
	end
end

This won't set it to a random position, though. This will make it move to either pos or essentially pos/2, which could be arbitrarily far away.

If you want to pick a random point in the vicinity,

while true do
	wait(1)
	local home = spawn.Position
	local distance = (script.Parent.Position - home).magnitude
	if distance > 15 then
		--print("Too far!")
		force.position = home
	else
		force.P = math.random(48000, 55000)

		-- Select a random offset in X and Z
		local offX = math.random() * 30 - 15
		local offZ = math.random() * 30 - 15
		-- Add that offset to my spawn position
		force.position = home + Vector3.new(offX, 0, offZ)
	end
end

In this case, the check to see if it went too far probably doesn't accomplish much, since it won't go very far anyway (it always picks a point near spawn even if it isn't the spawn exactly).

Thus the following is probably enough:

while wait(1) do
	local home = spawn.Position
	local offX = math.random() * 30 - 15
	local offZ = math.random() * 30 - 15
	force.position = home + Vector3.new(offX, 0, offZ)
end

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