So I am making a AI that can detect a player within a range and if the player is in the AI’s range long enough it detects it and something happens like prints "PLAYER TOO LONG IN RANGE INITIATING NUCLEAR WARHEAD PROCEDURE"
lol you get the idea right?
local character = script.Parent
local head = character:WaitForChild("Head")
local WaypointsFolder = workspace.CivWaypoints
local Waypoints = WaypointsFolder:GetChildren()
local inround = false
table.sort(Waypoints,function(a,b)
return a.Name < b.Name
end)
local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 7,
AgentCanJump = true,
AgentCanClimb = true,
})
local humanoid = character:WaitForChild("Humanoid")
local waypoints
character.PrimaryPart:SetNetworkOwner(nil)
local function followPath(destination)
-- Compute the path
local success, errorMessage = pcall(function()
path:ComputeAsync(character.PrimaryPart.Position, destination)
end)
if success and path.Status == Enum.PathStatus.Success then
waypoints = path:GetWaypoints()
for i,v in pairs(waypoints) do
humanoid:MoveTo(v.Position)
if v.Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
end
humanoid.MoveToFinished:Wait()
end
end
end
local angle = 45
local dist = 35
while true do
for idx = -angle,angle,5 do
local Origin = head.Position
local Destination = head.CFrame*CFrame.Angles(0,math.rad(idx),0)*CFrame.new(0,0,-dist)
local Direction = Destination.Position-Origin
local DetectionRay = Ray.new(Origin,Direction)
local Part,Pos = workspace:FindPartOnRay(DetectionRay,head.Parent,false,false)
if Part ~= nil then
if Part.Parent:FindFirstChild("Humanoid") or Part.Parent.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(Part.Parent or Part.Parent.Parent)
if player ~= nil and player:IsA("Player") then
humanoid.WalkSpeed = 0
game.ReplicatedStorage.Detection:FireClient(player,script.Parent.HumanoidRootPart,dist)
end
else
humanoid.WalkSpeed = 16
end
end
end
coroutine.resume(coroutine.create(function()
if inround then return end
inround = true
for Idx,Obj in pairs(Waypoints) do
followPath(Obj.Position)
local div = humanoid.WalkSpeed/2
wait((Obj.Position-character.PrimaryPart.Position).Magnitude/div)
end
inround = false
end))
wait()
end
this is my code and its not good it detects the player and stops the bot but its not that good like when its detecting the player it stops and when it stops detecting the player its just standing. Doesn’t move again.