# Randomizing raycast origin

Hi, im trying to use a big square part to spawn parts on the ground. How can i randomize the ray origin position on the X and Z axis?

My current script: (this script sends a ray towards the ground and spawns a part, the problem is that the ray comes from the middle of the part)

local spawner = script.Parent

while wait(0.1) do
local rayOrigin = spawner.Position
local rayDirection = Vector3.new(0, -200, 0)

local raycastParams = RaycastParams.new()
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

if raycastResult then
local part = Instance.new(“Part”, workspace)
part.Position = raycastResult.Position
part.Color = Color3.fromRGB(255, 0, 0)
part.Anchored = true
print(“spawned”)
end
end

Randomizing ray origin is very simple. Use `math.random`.

``````local x = (your number)
local y = math.random(1, 50) -- Or some other 2 numbers
local z = math.random (1, 50) -- Or some other 2 numbers

local rayOrigin = Vector3.new(x,y,z)
``````

If this works, please list as the Solution! Thanks!

I know how to randomize numbers the problem is that i don’t know how to get a random position from that part. I only want rays to be sent from that part.

Which part? If you’re asking about `spawner` then just do `spawner.Position`.

Yes im talking about the spawner part. I need a way to pick a random position -inside- that part.
I’ve tried doing this but it doesn’t work

local Height = spawner.Position.Y

local XSize = spawner.Size.X/2

local ZSize = spawner.Size.Z/2
local rayOrigin = spawner.Position * Vector3.new(math.random(-XSize,XSize),Height,math.random(-ZSize,ZSize))

Basically i want the raysOrigin to be random but to be restricted to that part. Red dots are supposed to be randomized rays.

I’ve tried doing this but i get an error

Unable to cast CoordinateFrame to Vector3

The error is for line 17 which is

local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

This is the full script

``````local spawner = script.Parent

function getRandomInPart(spawner)
local random = Random.new()
local randomCFrame = spawner.CFrame * CFrame.new(random:NextNumber(-spawner.Size.X/2,spawner.Size.X/2), random:NextNumber(-spawner.Size.Y/2,spawner.Size.Y/2), random:NextNumber(-spawner.Size.Z/2,spawner.Size.Z/2))
return randomCFrame
end

while wait(0.1) do
local rayOrigin = getRandomInPart(spawner)
local rayDirection = Vector3.new(0, -200, 0)

local raycastParams = RaycastParams.new()
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

if raycastResult then
local part = Instance.new("Part", workspace)
part.Position = raycastResult.Position
part.Color = Color3.fromRGB(255, 0, 0)
part.Anchored = true
print("spawned")
end
end
``````

Yes, that happens because unfortunately your function is returning a CFrame, not a Vector3

Yeah, i switched to Vector3 i dont get any error but the script also doesn’t work

This is the new getRandomInPart function

function getRandomInPart(spawner)
local random = Random.new()
local randomPosition = spawner.Position * Vector3.new(random:NextNumber(-spawner.Size.X/2,spawner.Size.X/2), random:NextNumber(-spawner.Size.Y/2,spawner.Size.Y/2), random:NextNumber(-spawner.Size.Z/2,spawner.Size.Z/2))
return randomPosition
end

Okay, check this out.

Hi, i’ve tried that but it only spawns them in the middle now

``````local Region = workspace.Region

local x = Region.Position.x
local z = Region.Position.Z

while wait(0.1) do

local xS = Region.Size.X/2
local zS = Region.Size.Z/2

local pos1 = math.random(x-xS,x+xS)
local pos2 = math.random(z-zS,z+zS)

local rayOrigin = Vector3.new(pos1, Region.Position.Y, pos2)
local rayDirection = Vector3.new(0, -500, 0)

local raycastParams = RaycastParams.new()
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

if raycastResult then
local part = Instance.new("Part", workspace)
part.Position = raycastResult.Position
part.Color = Color3.fromRGB(255, 0, 0)
part.Anchored = true
print("spawned")
end
end``````

Huh… I’m sorry, I don’t know either, I will maybe look to see if I can find out.

You got a lowercase x in

``````local x = Region.Position.x
``````

that might be why

I saw and fixed that but its not the issue, it still works the same if its a lowercase or uppercase X
I switched to uppercase and its the same.

I copy pasted that script into studio, and it seems to be working for me

That grey part is named Region

Yeah it works… I had to make a new part, it seems that one had a weird size lol. Thanks!