Raycast returning Different Instance and Distance

This is the most baffling issue I’ve ever had. I have zero earthly idea as to how this could be happening. I have a piece of code that runs constantly, checking if a player is within the range of another player’s spotlight. If so, they are slowed down.

The problem is that while it returns the proper instance, the distance is measured to something different. At the start of the round, each player is given a headlamp, cloned from a main headlamp in ReplicatedStorage. Each headlamp is welded to their head and is what the raycast checks for. When the headlamp is put on, it’s named uniquely to the player it’s given to (HeadlampPlayer1, HeadlampPlayer2, etc.). It’s then parented to it’s respective character.

When I print the RaycastResult.Instance and RaycastResult.Distance, the instance shows properly. It’ll say “HeadlampPlayer2”, but the distance every single time is measured to where the original headlamp in ReplicatedStorage is. It stays in ReplicatedStorage during playing, but the raycast is sent in the direction of the mother headlamp. It is intercepted only by things in the way of the mother Headlamp.

I’ve been toiling over this for a few hours now. I know exactly what the problem is but for the life of me can’t figure out why, and more importantly, how to fix it. I don’t get how the raycast is even aware of the mother Headlamp, since it is never referenced except for the creation of the headlamp.

Below is the code of my light system, it returns to the main script which does nothing except run the loop. I’ve tried slowing down the loop but that doesn’t fix it at all. Any and all help is greatly appreaciated and needed.

local lightmodule = {}

local Players = game:GetService("Players")

local lightsFolder = game.Workspace.Lights

local activeLights = 0
local activeLightArray = {}

local function LightGauge(player, light)
	
	if light then
		
		local pLight = light:FindFirstChild("PointLight") or light:FindFirstChild("SpotLight")
		local range = pLight.Range

		local raycastParams = RaycastParams.new()
		raycastParams.CollisionGroup = "Default"
		raycastParams.FilterDescendantsInstances = {player.Character:GetChildren()}
		raycastParams.FilterType = Enum.RaycastFilterType.Exclude

		local humrp = player.Character.HumanoidRootPart

		local rayDirection = (light.Position - humrp.Position)

		local raycastResult = workspace:Raycast(humrp.Position, rayDirection, raycastParams)

		if raycastResult then
			print(raycastResult.Distance)
			print(raycastResult.Instance)

			if raycastResult.Instance == light then

				if raycastResult.Distance <= range then
					return true
				else
					return false
				end

			else
				return false
			end

		else
			return false
		end
		
	end
	
end

lightmodule.RunLightCheck = function(player, gameRunning, survArray)

	if gameRunning == true then
		
		for i = 1, #survArray do
			
			local surv = survArray[i]
			local char = surv.Character
			
			local pLight = char:FindFirstChild("Headlamp".. surv.Name):FindFirstChild("Holder"):FindFirstChild("Source".. surv.Name)
			
			if LightGauge(player, pLight) == true and table.find(activeLightArray, pLight) == nil then
				print("player light found")
				 
				activeLights += 1
				player.Character.Humanoid.WalkSpeed -= 5
				table.insert(activeLightArray, pLight)
			else
				if LightGauge(player, pLight) == false and table.find(activeLightArray, pLight) then
					activeLights -= 1
					player.Character.Humanoid.WalkSpeed += 5
					table.remove(activeLightArray, table.find(activeLightArray, pLight))
				end
				
			end
		end
		
		print(activeLights)
		return activeLights
	else
		activeLights = 0
		table.clear(activeLightArray)
	end
end

return lightmodule

Forgot to mention two important things: While the game is running, I checked ReplicatedStorage and the mother Headlamp is never renamed, it just stays as “Headlamp” it never receives the player name appendage. Second, I thought that maybe the position of the new headlamp never registered for some reason and the script thought it was still at that original position. So in the script I hardcoded the CFrame of the headlamps to be on the heads of the players but that didn’t change a thing.

:GetChildren() returns a table, no need to wrap it, also you can just do Player.Character and it will include all of the children in the raycastfilter


is the headlamp anchored at all?

1 Like

the headlamp is not anchored in any way, it’s completely held together by welds

well I just figured it out and feel like a complete dumbass, I guess at one point I moved the source part of the headband, where the light comes out of, and broke the weld. This is also the part that is referenced for the script. So, it doesn’t get attached to the headband and falls off at the origin point of the mother headband.

anyway thanks for your help, I wouldn’t have looked over it again if it werent for you

lol glad you could find it

i was literally verbatim about to ask:

“just curious, could you not just check the players distance to each other (like hrp to hrp) because you just need to see if their in each others proximity? or use like LookVectors to detect where the player is looking as your raydirection?”

i didn’t send it because i was trying to FULLY understand what was wrong :sob:

1 Like

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