Why won't raycast work on this script?

So i’ve been having problems with the use of raycasting but the script below shows what it does.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local bullet = ReplicatedStorage:WaitForChild("Bullet")

local turret = script.Parent

local fireRate = 0.5
local bulletDamage = 10
local bulletSpeed = 150
local agroDistance = 100

while wait(fireRate) do
	-- Find the target, Detects if it's visible to shoot
	local target = nil
	for i, v in pairs(game.Workspace:GetChildren()) do
		local human = v:FindFirstChild("Humanoid")
		local torso = v:FindFirstChild("Torso")
		if human and torso and human.Health > 0 then
			if (torso.Position - turret.Position).magnitude < agroDistance then
				local bulletRay = Ray.new(turret.Position, (torso.Position - turret.Position).Unit * agroDistance)
				local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(bulletRay, {turret})
				if hit == torso then
					target = torso	
				else
				   print("Object in the way")
				end
				
			end
		end
	end
	if target then
		local torso = target
		-- turns the turret to face the target
		turret.CFrame = CFrame.new(turret.Position, torso.Position)
		
		local newBullet = bullet:Clone()
		newBullet.Position = turret.Position
		newBullet.Parent = game.Workspace
		newBullet.Velocity = turret.CFrame.LookVector * bulletSpeed
		newBullet.Touched:Connect(function(partHit)
			local human = partHit.Parent:FindFirstChild("Humanoid")
			if human then
				human:TakeDamage(bulletDamage)
			end
		end)
	end
end

but in this line:

if hit == torso then
					target = torso	
				else
				   print("Object in the way")
				end

the bullet won’t fire even though an object is not in the way

Video:

1 Like

I’m not sure about this, but maybe the ray hits the HumanoidRootPart instead of hitting the Torso and that causes the script to think that there’s an object in the way. So instead of only checking for torso, maybe check if hit is either the torso or the HumanoidRootPart. You could also probably just check if hit.Parent == v, because v seems to refer to a character. Then the if statement would accept any bodypart of that character, but I don’t know if you don’t want this behavior for some reason.
Also, if the turret has CanCollide set to true, maybe it’s possible that positioning the bullet inside the turret could cause some problems to the movement of the bullet.

Hi, I think the issue is with what parts its ignoring. Its set to only ignore “topPart” (since topPart is script.Parent which is turret). So try setting the ignore list to one of the options here (haven’t tested it but all options should work (considering you keep the same hierarchy)

  • script:FindFirstAncestorOfClass("Model")
  • Turret.Parent

(Remeber to add the {} since the ignorelist requires a table)

One recomendation i’d do when you get stuck in stuff like this is try to know what the ray “is hitting”, so you can then see what is in the way or what is stoping it

You could debug what’s going on by using position value returned from the ray and creating a part there and printing hit:GetFullName()

If you want the ray to only hit the players torso, why not use the Whitelist of raycasting?

for _,v in pairs(workspace:GetChildren()) do
    if v:FindFirstChild("Humanoid") and v:FindFirstChild("Torso") then
        local human, torso = v.Humanoid, v.Torso

        if human.Health > 0 then
            --put your if statements here im lazy to type them lol
            local bulletRay = Ray.new(turret.Position, (torso.Position - turret.Position).Unit * agroDistance)
            local hit, position = workspace:FindPartOnRayWithWhitelist(bulletRay, {torso})
        end
   end
end

Since these old Raycast functions are now deprecated, I actually reccomend you to use workspace:Raycast() with RaycastParams. Read about em’ here.

Wouldn’t that completely prevent the ray from hitting obstacles?

Right, my bad. The posts above are good alternatives so you can try them.

The ray did hit the humanoidrootpart which makes the script think the object was in the way. It worked after I fixed it. Thanks!