My ray will detect a humanoid even if there isn't one in the part it collided with?

I have a gun script that casts a ray when the Held value is true, if the ray detects a humanoid in it’s instance’s parent (ray.Instance.Parent) it damages that humanoid. But when I shoot at any object it damages a humanoid no matter what, for better comparison it’s like an aimbot gun. I find this to be the weirdest thing, so I decided to print ray.Instance.Parent.Name and whenever I shoot anything it prints “BASE” in the output. I don’t know why this is happening but if someone could help me that would be great! Thanks!

LocalScript inside of the gun:

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

local Held = false

local DelayPerBullet = 0.1

script.Parent.Activated:Connect(function()

Held = true

end)

script.Parent.Deactivated:Connect(function()

Held = false

end)

while wait() do

if Held == true then

wait(DelayPerBullet)

local mousePos = mouse.Hit.p

script.Parent.OnShoot:FireServer(mousePos)

end

end

ServerScript:

local Ammo = script.Ammo
Ammo.Value = 30
local MaxAmmo = script.MaxAmmo
MaxAmmo.Value = 30

game.Players.PlayerAdded:Connect(function(player)
	local Character = player.CharacterAdded:Wait()
	local Gun = Character:FindFirstChild("Gun") or player.Backpack:FindFirstChild("Gun")
	
	local Barrel = Gun.Barrel
	
	Gun.OnShoot.OnServerEvent:Connect(function(player, mousePos)
		
		if Ammo.Value <= MaxAmmo.Value then
			local midPoint = (Barrel.Position - mousePos)
				
			local ray = workspace:Raycast(midPoint, (mousePos - Barrel.Position).Unit * 100)
				
			if ray then
				local hitPart = ray.Instance
				print(ray.Instance.Parent.Name)
				if hitPart.Parent:FindFirstChild("Humanoid") then
					local Humanoid = hitPart.Parent.Humanoid
					if Humanoid ~= player.Character.Humanoid then
						Humanoid:TakeDamage(10)
					end
					
				end
			end
		else
			print("Not enough ammo")
		end
	end)
end)
1 Like

Your problem most likely lies in:
local hitPart = ray.Instance
That’s not how raycast hitscanning works, you should instead do:
local hitPart = workspace:FindPartOnRay(ray,Character)

Could you briefly explain to me what :FindPartOnRay does? I’ve looked around quite a bit but I feel like the developer hub leaves out a bit of information and I can’t seem to understand it.

its gives you the first variable the part, the position, the normal
like this

local targetPart, positionThatItWasHitOn, surfaceNormal = workspace:FindPartOnRay(ray)
there are more things it returns tho

:FindPartOnRay essentially gets the part that the ray hits, refer to the drawing:

ray

There’s nothing much more to it, unless if you mean the technical aspect, to which I can’t respond, because I simply don’t know. It has 4 parameters such as the ray that’s supposed to be used, things to ignore when firing the ray, something to do with terrain voxels and whether it ignores water or not.
Moonvayne beat me to it but it also returns the target part, position and surface.

1 Like

When I replace hit.Part = ray.Instance with local hitPart = workspace:FindPartOnRay(ray,Character) it gives me this error: Unable to cast RaycastResult to Ray

They are using WorldRoot:Raycast. Telling OP to replace their correct usage with outdated and deprecated methods is not constructive.

Excuse me, since when is WorldRoot:FindPartOnRay() deprecated and any worse than WorldRoot:Raycast?

1 Like

Since Roblox said it is. And the point is not that it’s better, but that your solution is just to replace like for like which doesn’t really solve OP’s issue. It just aligns their code with your own preferences.

stupid
Right. Of course, ROBLOX never bothered to update the wiki page and delayed the release update by a month. Not surprised at this point, considering how terrible the platform behaves anyway.
Thanks for notifying and correcting me, I have a few gun scripts to fix

No worries. The old functions will still work and will likely exist for a long time due to all the scripts that’s still use them.

The new method works quite differently and does take a moment to get your head around the fact it returns a RaycastResult instance

1 Like

Which humanoid gets damaged with this script? Do you think the raycast might be triggering for the gun itself and therefore is damaging the humanoid that used the gun?

You don’t seem to do any sort of filtering out the shooter’s own character.

Do you have any info on what the BASE part is?

I haven’t tested it with more than 2 characters yet, but whenever I shoot the gun at a part in workspace it seems to damage any other humanoid that isn’t the player that is shooting the gun’s humanoid. I have no clue what the BASE part is because there isn’t a part in my game with the name BASE and I’ve only been learning about rays for the past couple of days so filtering parts completely blew over my head.

I believe your issue to be the flawed logic of where your ray origin is. Midpoint is not actually returning the midpoint of anything, it’s a vector of where your barrel is relative to the mouse position, which isn’t going to help here.

Set the first parameter of Raycast to Barrel.Position instead, and see if that sorts the issue out.

That was the issue, thanks for your time man! That would probably be the last thing I would look at for this issue.

2 Likes