Evening everyone,
I’ve been working on making an FPS game, and I’ve wanted to create a raycast that fires an player-piercing beam. However, in one iteration of this code, I had a weird issue where the raycast would seemingly stop randomly, so I rewrote the whole piece of code.
local currentMap = workspace:FindFirstChild("CurrentMap")
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {
player.Character,
currentMap.SpecParts.Pass,
workspace.BeamFolder,
workspace.BloodFolder
}
local raycast = workspace:Raycast(pHead.Position, mouseHit.LookVector * 1000, raycastParams)
--Acceptable raycast
if raycast then
local humanoid = raycast.Instance.Parent:FindFirstChild("Humanoid")
local accessory = raycast.Instance:FindFirstAncestorOfClass("Accessory")
--Keep going through players until you hit an acceptable object
if humanoid or accessory then
--Create blood positions
local bloodPositions = {}
--Create new raycast params
local newRayParams = RaycastParams.new()
newRayParams.FilterType = Enum.RaycastFilterType.Exclude
newRayParams.FilterDescendantsInstances = table.clone(raycastParams.FilterDescendantsInstances)
--Check if there's a humanoid, if true, filter whole character model
if humanoid then
table.insert(newRayParams.FilterDescendantsInstances, humanoid.Parent)
table.insert(bloodPositions, raycast.Position)
else
table.insert(newRayParams.FilterDescendantsInstances, raycast.Instance)
end
local newRaycast = nil
--Repeat until no more player parts are found
repeat
newRaycast = workspace:Raycast(pHead.Position, mouseHit.LookVector * 1000, newRayParams)
if newRaycast then
humanoid = raycast.Instance.Parent:FindFirstChild("Humanoid")
accessory = raycast.Instance:FindFirstAncestorOfClass("Accessory")
if humanoid then
table.insert(newRayParams.FilterDescendantsInstances, humanoid.Parent)
table.insert(bloodPositions, newRaycast.Position)
else
table.insert(newRayParams.FilterDescendantsInstances, newRaycast.Instance)
end
end
until humanoid == nil and accessory == nil
raycast = newRaycast
VisManager.CreateBlood(bloodPositions)
end
local hitPart = raycast.Instance
print(hitPart)
--Destory part if it's supposed to break
if hitPart:IsDescendantOf(currentMap.SpecParts.Break) then
hitPart:Destroy()
end
--Create new VisBeam
VisManager.CreateBeam(pHead, mouseHit, raycast.Distance)
end
However due to this, a new issue popped up, during the player collision loop (located underneath the “Repeat until no more player parts are found” comment) I get a script timeout error when checking for the humanoid! When trying to debug it, it seems that the code is infinitely looping(?). When trying to print the raycast to check if it’s looping, it then states that that’s the reason the timeout is happening.
Video demonstration (shoot sorry just realized I left my music playing):
I’ve tried looking through the forums but I can’t find what I’m looking for, I’m completely baffled on how to fix this. Any help would be greatly appreciated!
Thank you!

