Ray between 2 players blocked for no reason. [SOLVED]

  1. What do you want to achieve?
    I want to cast a ray between two players heads on a local script, and return true if a part is not between them, and return false if a part is between them.

  2. What is the issue?
    When I test this, it works at first. But after moving away and then moving close and testing, sometimes it detects a part between us that is not between us at all.

(The white dot represents the result.Position of the ray if the ray hits a part.)
An example of it working fine

Example of it not working:
While I’m standing infront of her we activate the code again, but the result position hits the wall beside us for no particular reason. This is what I want to fix! I’m puzzled as to why it’s doing this.
image

  1. What solutions have you tried so far?
    I could not find any topics with this issue.
    I tried reversing part 1 and part 2.
    I tried re naming the variables.
    I tried changing from the depreciated Raycast system to the newer RayCast system.

Here is my code:

function rayrun(part1,part2)
	print(part1.Parent.Name .." | Part 1")
	print(part1.Position)
	print(part2.Parent.Name.." | Part 2")
	print(part2.Position)
	
	local IgnoreTable = {}
	table.insert(IgnoreTable,part1.Parent)
	table.insert(IgnoreTable,part2.Parent)
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.FilterDescendantsInstances = IgnoreTable
	raycastParams.IgnoreWater = true
	
	local p11 = part1.Position
	local p22 = part2.Position
	
	local result = workspace:Raycast(p11,p22, raycastParams)

	if result and result.Instance then
		print(result.Instance.Name.." hit is not nil")
		local p = Instance.new("Part")
		p.Anchored = true
		p.Material = Enum.Material.Neon
		p.CanCollide = false
		p.CanQuery = false
		p.Parent = part1.Parent
		p.Size = Vector3.new(0.5, 0.5,.5)
		p.Position = result.Position
		return false
	else 
		print(" hit is nil")
		return true
	end
end
rayrun(Player1.Character.Head,Player2.Character.Head)

Here is the printed result when it does not work properly:


It seems to get our positions right, but the ray is totally wrong, and shoots into the wall?

I really need help with this!

Here is the game:
(11) Test for granbaby - Roblox

If you want to test it out yourself, you can play. To activate the code just have another player say anything to you while within a 15 stud range of you.

1 Like

Don’t just detect for head. It’s unreliable because of character sizes, etc. Probably make a hitbox welded to the humanoidrootpart for it.
Or detect if a part is a child of the character.

I tried that already,
I used humanoid root part originally, but the same issue occurred.

Also, the ray is not trying to detect the other head, the ray ignores both characters and their parts.
The goal is to determine if it hits any parts along the path of the ray between both characters.

Try changing the line where you raycast to local result = workspace:Raycast(p11, (p22 - p11), raycastParams)

hmm. try looking into using WorldRoot | Roblox Creator Documentation
for your raycasting.

I used that originally, its depreciated so I switched to the new version which is using RayCast and setting the Blacklist param.

workspace:Raycast()'s second argument is a direction, not a position

you know what, for detecting if wall is a player or a plain old brick try using:

if rayresult:IsA("BasePart") then
       local isplayer = game.Players:GetPlayerFromCharacter(rayresult.Parent)

      if isplayer then
            --we now know part that is detected is a player
       else
             --we know given part is not a player.
      end
end

I am trying your solution right now in game, if you are available you should come to see for yourself.

This worked, I didn’t see your second post but as soon as I saw what you posted I realized my brain was just farting and I set the second param as a vector3 position and not a direction like you suggested.

Feeling super dumb now, considering I have used rays hundreds of times.
Thanks!

2 Likes