My movement script works like that:
go to part1.position if you arrived the position break the loop and go part2.position loop and do that for 18 times
So the issue is if npc catches the player in part4 while loop (or part10 it doesnt matter) Npc thinks or code "I need to go part4 because im in part4 while loop" but i dont want that, i want if npc catches the player while loops start over. (i hope i explained well)
Here is my npc movement code, i did that for 18 times i dont know if its a effect to performance
while wait(1.5) do local distance = (NPC.HumanoidRootPart.Position - part1.Position).magnitude if distance <= mag then break end NPC.Humanoid:MoveTo(part1.Position) end
Here is the event when npc catches the player
wait() local dum = game.Workspace.CellatBaba local players = game:GetService("Players") local lead = game.Players:WaitForChild("leaderstats", 1) local Remote = game.ReplicatedStorage.CheckPoint local t = game.Workspace.SpawnBase.TeleportPad game.Workspace.CellatBaba.Hitbox.Touched:Connect(function(hit) local Player = game.Players:GetPlayerFromCharacter(hit.Parent) if game.Players:GetPlayerFromCharacter(hit.Parent) then Remote:FireServer() game.Workspace.CellatBaba:SetPrimaryPartCFrame(CFrame.new(t.Position)) hit.Parent.Humanoid.Health = 0 end end)
I think I understand your question (thank you golddog for interpreting ;) ), I assume you have several waypoints that the character moves towards? in this case, I would have a list of these waypoints, loop through them, and run a function on each utilizing the same loop. if a player is caught I would restart the outer waypoint loop through some sort of signal, like a bindable, remote, or custom event. like so.
script 1:
local Caught = Instance.new("BindableEvent", ServerScripts) game.Workspace.CellatBaba.Hitbox.Touched:Connect(function(hit) local Player = game.Players:GetPlayerFromCharacter(hit.Parent) if game.Players:GetPlayerFromCharacter(hit.Parent) then Caught:Fire() Remote:FireServer() game.Workspace.CellatBaba:SetPrimaryPartCFrame(CFrame.new(t.Position)) hit.Parent.Humanoid.Health = 0 end end)
script 2:
local Caught = ServerScripts:WaitForChild("BindableEvent") function followPart(part) local connection connection = Caught.Event:Connect(function() connection:Disconnect() game.Workspace.CellatBaba:SetPrimaryPartCFrame(CFrame.new(t.Position)) return false -- return false if it did not reach destination end) while true do local distance = (NPC.HumanoidRootPart.Position - part.Position).magnitude if distance <= mag then break end NPC.Humanoid:MoveTo(part1.Position) wait(1.5) end return true end local waypoints = workspace.Waypoints:GetChildren() -- folder in workspace containing part1, part2 etc. until part18 for i = 1, #waypoints do if not followPart(waypoints[i]) then i = 0 -- restart from point 1 next loop end end
So i did something like that:
BindableEvent.Event:Connect(function(caught) game.Workspace.CellatBaba.Hitbox.Touched:Connect(function(hit) local Player = game.Players:GetPlayerFromCharacter(hit.Parent) if game.Players:GetPlayerFromCharacter(hit.Parent) then Remote:FireServer() game.Workspace.CellatBaba:SetPrimaryPartCFrame(CFrame.new(t.Position)) hit.Parent.Humanoid.Health = 0 end end) end) local Caught = BindableEvent.Event function followPart(part) local connection connection = Caught:Connect(function() connection:Disconnect() game.Workspace.CellatBaba:SetPrimaryPartCFrame(CFrame.new(t.Position)) return false -- return false if it did not reach destination end) while true do local distance = (NPC.HumanoidRootPart.Position - part.Position).magnitude if distance <= mag then break end NPC.Humanoid:MoveTo(part.Position) print(distance) wait(1.5) end return true end local waypoints = workspace.Going:GetChildren() -- folder in workspace containing part1, part2 etc. until part18 for i = 1, #waypoints do if not followPart(waypoints[i]) then i = 0 -- restart from point 1 next loop end end
but now the problem is npc not going to part1, npc going where he wants like random. Every time i play i see npc going somewhere else.