[Solved] Script Timeout over Raycast Loop

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!

In your repeat until loop, there is nothing stopping it from running once there stop being valid raycast results. You probably want this:

if newRaycast then
	...
else
	break
end

Oh shoot, yeah you’re right, let me see if that works…

Nope, still didn’t work. Seems that there isn’t a problem with null raycasts, at least with the specific situation.

Try printing some info with each raycast so you can see where it’s going once it “runs away”

You could use a tick variable to end the loop

local startTick = tick()

repeat
    -- yada
until (humanoid == nil and accessory == nil) or (tick() - startTick >= 5)

Okay got some info. Here’s what happens-

When hitting a body part, it finds the raycast, detects the humanoid, and repeats.

With an accessory it’s the same except it detects an accessory instead.

What does it do when it times out, though?

Spits me this, currently line 57 is printing the raycast.

Just tried it, it doesn’t give me the error, instead it crashed Studio.

AHA! I got it!

So it seems with the new raycast parameters, it’s not updating!!!
image

…but that doesn’t make sense, I’m using tablet.insert() to add the new part it touches to the table, is there something wrong on that front?

Could I get someone to help me with this?

Alright after a lot of nonsense, I found the problem.

I was accidently referring to the original raycast to get the humanoid and accessory, isn’t that something?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.