You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I want to make a monster path-find it’s way to a player. -
What is the issue? Include screenshots / videos if possible!
The monster path-finds to the players location but if the player moves the monster continues to walk to the players old location.
(I followed this video but once the monster finds a target it doesn’t pathfind which I want it to do
Line 60 to 63 is where the problems at
local monster = script.Parent
local humanoid = monster.Humanoid
local PathfindingService = game:GetService("PathfindingService")
local rs = game:GetService("ReplicatedStorage")
local jumpscareEvent = rs:WaitForChild("Remotes"):WaitForChild("Jumpscares"):WaitForChild("Jumpscare1")
local cameraToPlayerEvent = rs:WaitForChild("Remotes"):WaitForChild("cameraToPlayerEvent")
local playHeartbeat = rs:WaitForChild("Remotes"):WaitForChild("playHeartbeat")
local stopHeartbeat = rs:WaitForChild("Remotes"):WaitForChild("stopHeartbeat")
local heartbeatTarget
monster.PrimaryPart:SetNetworkOwner(nil)
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 80
local nearestTarget
for i, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (monster.HumanoidRootPart.Position - target.HumanoidRootPart.Position).magnitude
if distance < maxDistance then
nearestTarget = target
maxDistance = distance
end
end
end
return nearestTarget
end
local function getPath(destination)
local pathParams = {
["AgentHeight"] = 15,
["AgentRadius"] = 6.5,
["AgentCanJump"] = false
}
local path = PathfindingService:CreatePath(pathParams)
path:ComputeAsync(monster.HumanoidRootPart.Position, destination.Position)
return path
end
local function attack(target)
local distance = (monster.HumanoidRootPart.Position - target.HumanoidRootPart.Position).magnitude
local plr = game.Players:GetPlayerFromCharacter(target)
local path = getPath(target.HumanoidRootPart)
if distance > 8 then
if heartbeatTarget == nil then
playHeartbeat:FireClient(plr)
heartbeatTarget = plr.Name
elseif heartbeatTarget == plr.Name then
elseif heartbeatTarget ~= plr.Name and heartbeatTarget ~= nil then
stopHeartbeat:FireAllClients()
playHeartbeat:FireClient(plr)
heartbeatTarget = plr.Name
end
--THIS IS WHERE THE ISSUE IS
for i, waypoint in pairs(path:GetWaypoints()) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
else
local AttackAnim = humanoid:LoadAnimation(script.Attack)
AttackAnim:Play()
target.Humanoid.Health = 0
jumpscareEvent:FireClient(plr)
wait(0.5)
end
end
local function walkTo(destination)
local path = getPath(destination)
for index, waypoint in pairs(path:GetWaypoints()) do
local target = findTarget()
if target and target.Humanoid.Health > 0 then
print("New Target: ", target.Name)
attack(target)
humanoid.WalkSpeed = humanoid.AttackingSpeed.Value
break
else
humanoid.WalkSpeed = humanoid.PatrolSpeed.Value
humanoid:MoveTo(waypoint.Position)
stopHeartbeat:FireAllClients()
heartbeatTarget = nil
humanoid.MoveToFinished:Wait()
end
end
end
local function patrol()
local waypoints = workspace:WaitForChild("Waypoints"):GetChildren()
local randomNum = math.random (1, #waypoints)
walkTo(waypoints[randomNum])
end
while wait(.001) do
patrol()
end