Check if a spotlight is shining on someone

I wanted to shine a spotlight over a dark area, and when the light shines over someone, they get “caught”, and thrown in prison.

I was thinking maybe I could use a cone and .Touched, but I was wondering if there was an alternative?

You can try to raycast from the character in the LookVector’s direction.

check the look vector thing

local Character -- the character
local Spotlight: Model -- a model i think

local function IsTouching()
	local Blacklist = {Cone}
	local RootPart = Character:FindFirstChild("HumanoidRootPart")

	for i, Item in pairs(Character:GetDescendants()) do
		if Item:IsA("BasePart") and Item ~= RootPart then
			table.insert(Blacklist, Item)
		end
	end

	local RaycastParam = RaycastParams.new()
	RaycastParam.FilterType = Enum.RaycastFilterType.Blacklist
	RaycastParam.FilterDescendantsInstances = Blacklist

	local Raycast = workspace:Raycast(
		RootPart.Position, 
		Spotlight:GetPivot().LookVector * 1000,
		RaycastParam
	)

	if Raycast and Raycast.Instance == RootPart then
		return true
	end
	
	return false
end

I mean, that only checks whether a ray can shoot from the rootpart to the spotlight part, right? it doesn’t actually check whether you’re in the light, correct?

– Check if the player is in the cone
while wait(0.5) do
if IsTouching() then
– do something
end
end

You could use Workspace.Raycast to cast a ray from the cone’s tip to the character’s root part.
local Character = game.Players.LocalPlayer.Character
local RootPart = Character.PrimaryPart – The character’s root part
local Cone = workspace.Cone

– Cast a ray from the cone’s tip to the character’s root part
local Raycast = workspace:Raycast(
Cone.Tip.Position,
RootPart.Position - Cone.Tip.Position,
Cone.Tip
)

– If the raycast hits the character’s root part, the character is in the cone
if Raycast and Raycast.Instance == RootPart then
print(“Character in cone”)
end

Note that if you want to cast a ray through a part, that part has to be passed to Raycast as the third argument.

Thanks, but I got by using Dot Product instead

1 Like