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