Hi all,
So recently I created an AI should walk to certain locations around my map, and if any player approaches within a certain radius of it it should begin to chase the player. I’ve got both functions coded, butI just need help combining them. For example, if the entity is in the middle of its randomWalk function and a player walks within its radius, it should immediately begin to chase the player. I’ve tried the code
local enemyDetectionHitbox = script.Parent.hitboxFolder.EnemyDetectionHitbox --if a player touches the hitbox part, the AI will begin to chase the player
enemyDetectionHitbox.Touched:connect(chasePlayer()) --chasePlayer is the function determines if the AI will chase the player
Although I expected the chasePlayer() function to run whenever touched, it only runs once at the beginning of the script, and never again. I could also call the chasePlayer() function dozens of times within the randomWalk() function (not shown in block of code), but not only is that a horrible practice, I know there are many other ways to code this, I just don’t know how. Here’s my entire code if it helps:
TL;DR: Title
local runService = game:GetService("RunService")
local playerService = game:GetService("Players")
local pathFindingService = game:GetService("PathfindingService")
local humanoid = script.Parent
local rootPart = script.Parent.PrimaryPart
local headPart = script.Parent.Head
local torsoPart = script.Parent.Torso
local hitboxFolder = script.Parent.Hitboxes
local walkSpeed = humanoid.Humanoid.WalkSpeed
local enemyDetectionHitbox = hitboxFolder.EnemyDetectionHitbox
local eventDetectionHitbox = hitboxFolder.EventDetectionHitbox
local pathFindingHitbox = hitboxFolder.PathfindingHitbox
local FOV = 135
local sight = 45
local eventSight = 135
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset/?id=180426354" -- Walk
local animationTrack = humanoid.Humanoid:LoadAnimation(animation)
animationTrack:Play()
--setup
enemyDetectionHitbox.Size = Vector3.new(enemyDetectionHitbox.Size.X, sight, sight)
eventDetectionHitbox.Size = Vector3.new(enemyDetectionHitbox.Size.X, eventSight, eventSight)
function randomWalk(attempts)
--enemyDetectionHitbox.Touched:Connect(chasePlayer())
--detects all parts in range and filters out every part but entityblock
local touchingParts = workspace:GetPartsInPart(pathFindingHitbox)
local allPaths = {}
local availablePaths = {}
walkSpeed = 16
for i,v in pairs (touchingParts) do
if v.Name == "EntityBlock" then
table.insert(allPaths, v)
end
end
--looks for suitable pathfind block
for i = 1, #allPaths do
local rayOrigin = torsoPart.Position
local rayDestination = allPaths[i].Position
local rayDirection = rayDestination - rayOrigin
local rayParameters = RaycastParams.new()
rayParameters.FilterDescendantsInstances = {script.Parent}
rayParameters.FilterType = Enum.RaycastFilterType.Exclude
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, rayParameters)
--humanoid:SetPrimaryPartCFrame(CFrame.Angles(0, math.rad(rayDirection.Y), 0))
if raycastResult ~= nil then
if raycastResult.Instance == allPaths[i] then --if raycastResult.Instance == allPaths[i] then
table.insert(availablePaths, allPaths[i])
end
end
end
if #availablePaths ~= 0 then --detects if there are any available paths
local pathNumber = math.random(1, #availablePaths)
local determinedPath = availablePaths[pathNumber]
local path = pathFindingService:FindPathAsync(torsoPart.Position, determinedPath.Position)
local waypoints = path:GetWaypoints()
for i,v in pairs(waypoints) do
humanoid.Humanoid:MoveTo(v.Position)
humanoid.Humanoid.MoveToFinished:Wait()
end
attempts = 0
else
humanoid:PivotTo(humanoid:GetPivot() * CFrame.Angles(0, rootPart.Orientation.Y - math.rad(180), 0))
attempts = attempts + 1
if attempts > 3 then
task.wait(0.1)
end
end
randomWalk(attempts)
end
function chasePlayer()
local playersInRange = workspace:GetPartsInPart(enemyDetectionHitbox)
print("run")
walkSpeed = 20
for i,v in pairs(playersInRange) do
if v.Name == "HumanoidRootPart" then
if playerService:FindFirstChild(v.Parent.Name) ~= nil then
local target = workspace:FindFirstChild(v.Parent.Name)
print(target.Name)
local path = pathFindingService:FindPathAsync(torsoPart.Position, target.HumanoidRootPart.Position)
local waypoints = path:GetWaypoints()
for i,v in pairs(waypoints) do
humanoid.Humanoid:MoveTo(v.Position)
humanoid.Humanoid.MoveToFinished:Wait()
end
else
randomWalk(0)
end
end
end
randomWalk(0)
end
--runService.Heartbeat:Connect(randomWalk())
task.wait(2.5)
enemyDetectionHitbox.Touched:connect(chasePlayer())
randomWalk(0)