What do you want to achieve? Keep it simple and clear!
when other npc moveToFinished:wait() happen it will not change waypoint for other npc
What is the issue? Include screenshots / videos if possible!
when other npc moveToFinished:wait() happen it will change waypoint for other npc
i want to make that the npc spawns and if they are close to player then they chase him but if not then they go to random waypoints but when other npc MoveToFinished:wait() happens then all of the other npc also change random waypoints before they even complete the MoveTo
local Enemy = {}
local RoundStarted = false
local runService = game:GetService("RunService")
local Range = 60
local attackRange = 5
local debounce = false
local runService = game:GetService("RunService")
local replicatedStorage = game.ReplicatedStorage
local enemyFolder = replicatedStorage.Enemies
local darkEnemies = enemyFolder.darkEnemies
local Players = game:GetService("Players")
local function FindNearestPlayer(Origin: Vector3) :Player?
local nearestPlayer = nil
local shortestDistance = Range
for _, player in ipairs(Players:GetPlayers()) do
local character = player.Character
if character and character:FindFirstChild("HumanoidRootPart") then
local distance = (character.HumanoidRootPart.Position - Origin).Magnitude
if distance < shortestDistance then
shortestDistance = distance
nearestPlayer = player
end
end
end
return nearestPlayer
end
function Enemy.Init()
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
if RoundStarted then return end
RoundStarted = true
task.wait(10)
for i, enemy in darkEnemies:GetChildren() do
if enemy:IsA("Model") then
local newEnemy = enemy:Clone()
newEnemy.Parent = workspace
newEnemy.HumanoidRootPart.CFrame = game.Workspace.SpawnLocation.CFrame * CFrame.new(0,5,0)
task.spawn(function()
local Humanoid: Humanoid = newEnemy.Humanoid
local HRP = Humanoid.RootPart
while true do
local nearestPlayer = FindNearestPlayer(newEnemy.HumanoidRootPart.Position)
if nearestPlayer then
Humanoid:MoveTo(nearestPlayer.Character.HumanoidRootPart.Position)
print("a monster is now chasing a player")
task.wait(0.1)
else
local waypoints = newEnemy:FindFirstChild("Waypoints"):GetChildren()
local randomNumber = math.random(1, #waypoints)
Humanoid:MoveTo(waypoints[randomNumber].Position)
Humanoid.MoveToFinished:Wait()
print("move to finished")
task.wait(2)
end
end
end)
end
end
end)
end)
end
return Enemy
Hey, sorry for the delay. I’ve been busy offline today xd
Anyway, please use this server script inside of your NPC.
local RoundStarted = false
local Range = 60
local attackRange = 5
local debounce = false
local replicatedStorage = game.ReplicatedStorage
local enemyFolder = replicatedStorage.Enemies
local darkEnemies = enemyFolder.darkEnemies
local Players = game:GetService("Players")
local waypoints = script.Parent:FindFirstChild("Waypoints"):GetChildren()
local randomNumber = math.random(1, #waypoints)
local waypoint = waypoints[randomNumber]
local function FindNearestPlayer(Origin: Vector3) :Player?
local nearestPlayer = nil
local shortestDistance = Range
for _, player in ipairs(Players:GetPlayers()) do
local character = player.Character
if character and character:FindFirstChild("HumanoidRootPart") then
local distance = (character.HumanoidRootPart.Position - Origin).Magnitude
if distance < shortestDistance then
shortestDistance = distance
nearestPlayer = player
end
end
end
return nearestPlayer
end
local function Enemy()
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
if RoundStarted then return end
RoundStarted = true
task.wait(10)
local newEnemy = script.Parent:Clone()
newEnemy.Parent = workspace
newEnemy.HumanoidRootPart.CFrame = game.Workspace.SpawnLocation.CFrame * CFrame.new(0,5,0)
task.spawn(function()
local Humanoid: Humanoid = newEnemy.Humanoid
local nearestPlayer = FindNearestPlayer(newEnemy.HumanoidRootPart.Position)
if nearestPlayer then
Humanoid:MoveTo(nearestPlayer.Character.HumanoidRootPart.Position)
print("a monster is now chasing a player")
task.wait(0.1)
else
repeat wait()
Humanoid:MoveTo(waypoint.Position)
until Humanoid.MoveToFinished:Wait()
end
end)
end)
end)
end
Enemy()
waypoint.Touched:Connect(function(v)
if v.Parent:IsA("Model") and v.Parent.Name == script.Parent.Name then
task.spawn(function()
local randomNumber = math.random(1, #waypoints)
local waypoint = waypoints[randomNumber]
local Humanoid: Humanoid = script.Parent.Humanoid
local nearestPlayer = FindNearestPlayer(script.Parent.HumanoidRootPart.Position)
if nearestPlayer then
Humanoid:MoveTo(nearestPlayer.Character.HumanoidRootPart.Position)
print("a monster is now chasing a player")
task.wait(0.1)
else
repeat wait()
Humanoid:MoveTo(waypoint.Position)
until Humanoid.MoveToFinished:Wait()
end
end)
end
end)
A couple things to note such as having NPCs have unique names and/or having same and/or similar NPCs with different waypoints to avoid interference and/or overlapping of function triggers because of met criteria in code upon execution via activation within record.
thanks for the code but if i put the npc in workspace.camera then both npc script will work and when the cloned npc touch a waypoint the npc at camera moves to a waypoint
you’re better off using remoteevents then so that when fired to the server, the script activates. For example, if number of players per game.Players absolutely equals x then fire remote event to the server and the server script will activate once that remote event is fired.
thank you for helping me and im sorry it took so much time but that was because the script still had problems so i needed to fix it and i thought that i would never be able to fix it but thanks to you i would never fix this issue because i did not thought of doing touch connection.
so yeah thank you for fixing the issue and now the enemies finally work.
local Range = 60
local attackRange = 5
local debounce = false
local replicatedStorage = game.ReplicatedStorage
local event: BindableEvent = replicatedStorage.Events.SpawnEnemy
local enemyFolder = replicatedStorage.Enemies
local darkEnemies = enemyFolder.darkEnemies
local Players = game:GetService("Players")
local waypoints = script.Parent:FindFirstChild("Waypoints"):GetChildren()
local randomNumber = math.random(1, #waypoints)
local waypoint = waypoints[randomNumber]
local function FindNearestPlayer(Origin: Vector3) :Player?
local nearestPlayer = nil
local shortestDistance = Range
for _, player in ipairs(Players:GetPlayers()) do
local character = player.Character
if character and character:FindFirstChild("HumanoidRootPart") then
local distance = (character.HumanoidRootPart.Position - Origin).Magnitude
if distance < shortestDistance then
shortestDistance = distance
nearestPlayer = player
end
end
end
return nearestPlayer
end
local function Enemy()
local newEnemy = script.Parent
task.spawn(function()
local Humanoid: Humanoid = newEnemy.Humanoid
local nearestPlayer = FindNearestPlayer(newEnemy.HumanoidRootPart.Position)
if nearestPlayer then
Humanoid:MoveTo(nearestPlayer.Character.HumanoidRootPart.Position)
print("a monster is now chasing a player")
task.wait(0.1)
else
repeat wait()
Humanoid:MoveTo(waypoint.Position)
print("repeat")
until Humanoid.MoveToFinished:Wait()
print("Finished")
end
end)
end
Enemy()
local isMoving = false
for i, wp in ipairs(waypoints) do
wp.Touched:Connect(function(v)
print("Touched")
if v.Parent:IsA("Model") and v.Parent.Name == script.Parent.Name and not isMoving then
isMoving = true
task.spawn(function()
-- Pick a new random waypoint different from the current one
local randomNumber
repeat wait()
randomNumber = math.random(1, #waypoints)
waypoint = waypoints[randomNumber]
until waypoint ~= wp
task.delay(1, function()
isMoving = false
end)
local Humanoid: Humanoid = script.Parent.Humanoid
local nearestPlayer = FindNearestPlayer(script.Parent.HumanoidRootPart.Position)
if nearestPlayer then
Humanoid:MoveTo(nearestPlayer.Character.HumanoidRootPart.Position)
print("a monster is now chasing a player")
task.wait(0.1)
else
repeat wait()
Humanoid:MoveTo(waypoint.Position)
print("repeat touched")
until Humanoid.MoveToFinished:Wait()
print("Finished touched")
end
print(wp)
end)
else
print("is moving")
end
end)
end