Eye kills player upon being spotted

Hi there! In this topic, I want to talk about a mechanic that I’ve always wanted, but haven’t gotten to making it because of my lack of experience with coding.

Basically, An eye that surveys the room, and when it spots a player, aims at them, and kills them slowly.

I know it has something to do with Rays and such, but I can never translate/form it into a script, I don’t really know how to code that well :sweat_smile:. If anyone can give me either a short script, or tips on how to make it myself, that would be greatly appreciated. Thank you in advance for the help!

1 Like

This is actually pretty easy to do!
First, you will need to get all players within a certain radius. You can do this by looping through all the players and checking the distance between their character and the eye.

After you get a player, you need to make the eye aim at them. You can use CFrame.lookAt for this.
Then, do a raycast from the eye to the player. If it hits, damage their humanoid.

1 Like

So basically, what I do is make a loop to check if a player/s is inside the max distance/radius of the eye, then check with a raycast if it hits a player. If it does, then I make it so it damages them? Also you can probs see I’m pretty bad at scripting hehe :sweat_smile:. Thanks anyways mate!

1 Like

I’m not them, but yes.

The events should go in this order:

  • Player is within distance of eye, can be done with a loop and magnitude checks between the eye and the players character, more specifically the players HumanoidRootPart.
  • Eye can actually see the player, raycast from the eye to the players character.
  • If the RaycastResult is a descendant of the players character, like its head, HumanoidRootPart, etc, then have the eye face them using Cframe.lookAt.
  • Start dealing damage!

I’m using this script I got from someone else for the raycast script, and it works. But I don’t know how I can make it so that it hurts the player, and surveys the area as well. Plus, it also snaps in place. Here’s the script:

local TeddyAI = script.Parent


local function getHumPos()
	return (TeddyAI.Position)
end

local function findPotentialTarget()
	local players = game.Players:GetPlayers()
	local maxDistance = 100
	local nearestTarget

	for index, player in pairs(players) do
		if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
			if player.Character.Humanoid.Health > 0 then
				local target = player.Character
				local distance = (TeddyAI.Position - target.HumanoidRootPart.Position).Magnitude

				if distance < maxDistance then
					nearestTarget = player
					maxDistance = distance
				end
			end
		end
	end

	return nearestTarget
end
function canSeeTarget(target)
	if target and target:FindFirstChild("HumanoidRootPart") then
		local origin = TeddyAI.Position
		local direction = (target.HumanoidRootPart.Position - TeddyAI.Position).unit * 100
		local ray = Ray.new(origin, direction)
		local ignoreList = {TeddyAI}

		local hit, pos = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)

		if hit then
			if hit:IsDescendantOf(target) then
				if target.Humanoid.Health > 0 then
					local unit = (target.HumanoidRootPart.Position - getHumPos()).Unit
					local lv = TeddyAI.CFrame.LookVector
					local dp = unit:Dot(lv)

					if dp > 0 then
						return true
					end					
				end
			end
		else
			return false
		end	
	end
end

while true do
	local target = findPotentialTarget()
	if target and canSeeTarget(target.Character) and target.Character.Humanoid.Health > 0 then
		TeddyAI.CFrame = CFrame.lookAt(script.Parent.Position, target.Character.HumanoidRootPart.Position, TeddyAI.CFrame.UpVector)
	else
	end
	game:GetService("RunService").Heartbeat:Wait()
end