'Handle' detected when raycasting between bot and players

I was making an AI in Roblox that raycasts to the player if in range and if line of sight, kills the player.

Yesterday, while working on it, I found out that raycasting is not working like it is supposed to do. In fact: Raycasting keep detecting this new ‘Handle’ part that is plastic. When I printed out ray result, it looks like this

RaycastResult{Handle @ -43.245182, 3.07947659, 96.0326767; normal = -0.598692536, 0.0684300214, -0.798050582; material = Plastic}

Here is the code, the part that does the raycasting is in the function in beyond line 50.

local Players = game:GetService('Players')
local RunService = game:GetService('RunService')
local PathfindingService = game:GetService('PathfindingService')
local me = script.Parent
local human = me.Humanoid
local rootpart = me.HumanoidRootPart
rootpart:SetNetworkOwner(nil)

local function AddPartFromParentToTable(parent,totable)
	for i,child in pairs(parent:GetChildren()) do
		if child:IsA('BasePart') then
			table.insert(totable,child)
		end
	end
end

local function ComputeNewPath(path,origin,destination)
	local success,err
	local count = 0
	repeat
		count += 1
		success, err = pcall(function()
			path:ComputeAsync(origin, destination)
		end)
	until success and path.Status == Enum.PathStatus.Success or count == 3
end

local function CreateNewPath(origin,destination)
	local path = PathfindingService:CreatePath({
		AgentRadius = 3,
		AgentHeight = 6,
		AgentCanJump = false,
		Costs = {
			Snow = math.huge,
			Metal = math.huge,
		},
	})
	ComputeNewPath(path,origin,destination)
	return path
end

local function MoveTowardsDestination(path)
	for _,waypoint in path:GetWaypoints() do
		human:MoveTo(waypoint.Position)
		human.MoveToFinished:Wait()
	end
end

local function CheckProximity(targetroot)
	if (targetroot.Position - rootpart.Position).Magnitude < 10000 then
		local rootcharacter = targetroot.Parent
		
		local bodiesexempt = {}
		
		AddPartFromParentToTable(rootcharacter,bodiesexempt)
		AddPartFromParentToTable(me,bodiesexempt)
		
		local rayparams = RaycastParams.new()
		rayparams.FilterDescendantsInstances = bodiesexempt
		rayparams.FilterType = Enum.RaycastFilterType.Exclude
		
		local raydirection = targetroot.Position - rootpart.Position
		
		local result = workspace:Raycast(rootpart.Position,raydirection,rayparams)
		print(result)
		if not result then
			print('I am near target')
			targetroot.Parent.Humanoid:TakeDamage(100)
			return true
		end
	end
end

local function FindNearestPlayer()
	local nearestdistance,nearesttarget
	for i,player in pairs(Players:GetPlayers()) do
		if player.Character then
			local char = player.Character
			if char:FindFirstChild('HumanoidRootPart') and char.Humanoid.Health ~= 0 then
				local charpos = char.HumanoidRootPart.Position
				if nearestdistance == nil or (charpos-rootpart.Position).Magnitude < nearestdistance then
					nearestdistance = (charpos-rootpart.Position).Magnitude
					nearesttarget = player
				end
			end
		end
	end
	return nearesttarget
end

local targetkilled
local function ActivateAI()
	while true do
		task.wait()
		local player = FindNearestPlayer()
		if player then
			local character = player.Character
			local playerroot = character.HumanoidRootPart
			
			local origin = rootpart.Position
			local destination = playerroot.Position
			local newpath = CreateNewPath(origin,destination)
			
			local runinstance
			runinstance = RunService.Heartbeat:Connect(function()
				local result = CheckProximity(playerroot)
				if result then
					human:MoveTo(rootpart.Position)
					targetkilled = true
					runinstance:Disconnect()
				end
			end)
			
			local allwaypoints = newpath:GetWaypoints()
			for i,waypoint in pairs(allwaypoints) do
				if targetkilled then
					targetkilled = nil
					break
				end
				human:MoveTo(waypoint.Position)
				human.MoveToFinished:Wait()
			end
			runinstance:Disconnect()
		end
	end
end

ActivateAI()```

Please do not ask people to write entire scripts or design entire systems for you. If you can't answer the three questions above, you should probably pick a different category.

Without reading much into the code, they’re probably accessories since they are not direct children of Character models. You can just do this instead:

local bodiesexempt = {
    table.unpack(me:GetDescendants()),
    table.unpack(rootcharacter:GetDescendants())
}

For the future, use prints or watch + breakpoints for debugging.

1 Like

you need to exclude parts that count in the ray cast, Handle is the first part that your raycast is hitting because it’s the closest possible part to the point you’re ray casting from.

RaycastParams | Documentation - Roblox Creator Hub

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