How could I make an A.I that runs from light?

Hi,

I want to make an A.I that will run away when light is present. My idea is to have a button that you click, in which a short burst of light happens. When this happens, the Enemy A.I that is chasing you (if its in proximity) will run away (preferably a pretty far distance). How would I go about doing that?

You might be able to make a BoolValue inside of the light to determine if it’s actually on when you want the npc/ai to run away if it’s in a certain distance of the light, if the button is clicked and the light is off it will be on and will change the value of the BoolValue to true meaning that the light is currently on. And the other way around, then add a script that checks the distance/magnitude between the light’s part parent. And if the BoolValue is true the npc/ai will run at a specificed object at a certain place. I don’t really have another way for it to run like randomly away but I think this could be a way.

If you are gonna have a button that you click, you can just put a check in there if the npc is close enough by doing magnitude (light.Position - npc.humanoidrootpart.Position).Magnitude, then if he is close enough make him run the other way.

local RunService = game:GetService("RunService")

local HRP = script.parent.HumanoidRootPart

local Lights = {}
for i,v in pairs(game.workspace:GetDescendants()) do
    if v:IsA("Light") then
        table.insert(Lights,1,v)
    end
end

game.workspace.DescendantAdded:Connect(function(Light)
    if Light:IsA("Light") then
        table.insert(Lights,1,v)
    end
end)

RunService.HeartBeat:Connect(function()
    local ClosestLightPos = nil
    local ClosestDistance = 100
    for i,v in pairs(Lights) do
         if v.Parent == nil then
             table.remove(Lights,i)
             continue
         end
         
         local Position = v.parent.Position
         local Distance = (Position - HRP.Position).Magnitude
         if distance < ClosestDistance then
             ClosestDistance = Distance
             ClosestLightPos = Position
         end
    end
    
    if ClosestLightPos then
        local Dir = HRP.Position + (HRP.Position - ClosestLightPos)*10
        script.parent.Humanoid:WalkTo(Dir)
    end
end)

This would do it

I cleaned up your code a little =)

Summary
local RunService = game:GetService("RunService")
local HRP = script.Parent.HumanoidRootPart
local Humanoid = script.Parent.Humanoid

--Add all lights in workspace to a table
--Make sure each light is inside of a part
local Lights = {}
local function NewLight(v)
   if v:IsA("Light") then
        table.insert(Lights, v)
    end
end
for i, v in ipairs(workspace:GetDescendants()) do
    NewLight(v)
end
workspace.DescendantAdded:Connect(NewLight)

--Every frame, check whether a light is close
local CheckFrequency = 1 / 20 --How long to wait inbetween each check in seconds
local LastCheck = os.clock()
RunService.Heartbeat:Connect(function()
    --Check if enough time has passed since the last check
    if os.clock() - CheckFrequency < LastCheck then
        return
    end
    LastCheck = os.clock()

    --Get the nearest light
    local ClosestLightPos = nil
    local ClosestDistance = 100
    for i, v in ipairs(Lights) do
         if not v:IsDescendantOf(workspace) then
             table.remove(Lights, i)
             continue
         end
         
         local LightPosition = v.Parent.Position
         local Distance = (LightPosition - HRP.Position).Magnitude
         if Distance <= ClosestDistance then
             ClosestDistance = Distance
             ClosestLightPos = Position
         end
    end
    
    --Move away from the nearest light (if there is one)
    if ClosestLightPos then
        local Dir = HRP.Position + (HRP.Position - ClosestLightPos) * 10
        Humanoid:MoveTo(Dir)
    end
end)

local ClosestLightPos = nil
Is the same as:
local ClosestLightPos

Thx, I was in school at that moment, didn’t have alot of time.

2 Likes

Well yes, but does it realy matter if it’s the same?

1 Like

local ClosestLightPos = nil provides emphasis for the = nil part. If I were to do something like this:

local Number
if Number2 == 1 then
   Number = 2
else
   Number = 1
end

then I would not put = nil because it is not going to equal nil under any circumstance - the value is always set, and the only reason for the initialisation is make the variable accessible even after the if statements.

However, if I were to do something like this:

local Number = nil
if Number2 == 1 then
   Number = 2
end

then I would put = nil because if Number2 does not equal 1, then Number will equal nil.

In conclusion, your mother.

1 Like