How would i make this npc detect light

Basically , This is rake AI in short. i want if a watch tower, torch, Anything that shows light the npc will go to it if its in 100 distance range

function GetLightsToChase()
	local Lights = game.Workspace.Tower:GetChildren()
	for i,v in pairs (Lights) do
		if v:IsA("SpotLight") then
			if v.Enabled == true then
			--I will make him move
			print("Found")


			end
		end
	end
end

GetLightsToChase()
``` yes i dont know how to do it that much
2 Likes

First, I think a SpotLight needs to be a direct child to a BasePart or Attachment, it couldn’t be directly a child of the model, so you might need to change the line to:

local Lights = workspace.Tower:GetDescendants()

Then to detect the distance between the NPC and the light, just compare the position of the NPC model’s PrimaryPart to the position of the parent of the SpotLight object with something like this:

local Distance = (NPC.PrimaryPart.Position - v.Parent.Position).Magnitude
1 Like

Try this function. Put a table of all the lights you want to detect. Also put the char(model) of the NPC or player you would like to test.

function IsCharInLight(char,lights)
	local root = char:FindFirstChild("HumanoidRootPart")
	if root then
		for i,v in pairs(lights) do
			local lightParent = v.Parent
			if lightParent:IsA("BasePart") then
				if (root.Position-v.Parent.Position).Magnitude <= v.Range then
					return true
				end
			end
		end
		return false
	end
	return false
end
1 Like