-
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. -
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.
-
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.