I’m attempting to fire a ray down from a height above a selected spawn location but it’s ignoring the red part and casting straight through to the spawnpoint. Then it just spawns the part. I’ve tried checking for instance.Name of the part it hits but it jsut says SpawnArea.
I’m obviously missing something as I’ve tried both excluding all the parts in the StartArea and including only parts in the WorldAssets folder, even the SpawnArea itself but to no avail. It just seems to spawn parts where it wants to.
local function getPosition(): Vector3
local maxAttempts = 10
local spawnPoint
for i = 1, maxAttempts do
local randomX = spawnArea.Position.X + math.random(-spawnArea.Size.X / 2, spawnArea.Size.X / 2)
local randomY = spawnArea.Position.Y
local randomZ = spawnArea.Position.Z + math.random(-spawnArea.Size.Z / 2, spawnArea.Size.Z / 2)
spawnPoint = Vector3.new(randomX, randomY, randomZ)
local origin = spawnPoint + Vector3.new(0, 1000, 0) --100 studs above the ground
local destination = spawnPoint
local direction = destination - origin
local params = RaycastParams.new()
params.FilterDescendantsInstances = {spawnArea.Parent}
params.FilterType = Enum.RaycastFilterType.Include
params.CollisionGroup = "Raycast"
local raycastResult = workspace:Raycast(origin, direction, params)
print(raycastResult.Instance.Name)
if not raycastResult or raycastResult.Instance.Name ~= spawnArea.Name then
print("Not correct result, trying again")
raycastResult = nil
task.wait()
getPosition()
else
--print(raycastResult.Instance.Name)
spawnPoint = Vector3.new(raycastResult.Position.X, raycastResult.Position.Y, raycastResult.Position.Z)
return spawnPoint
end
end
return Vector3.new(0, 5, 0) --if broken spawn here
end



