Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

When npc catches the player npc movement code should be start over (?)

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)	

5 comments

0

sorry, what do you mean when you talk about part1, part2, part4 and part10?

0

They pointing where npc will go

0

If i want to move npc to part1 i can do "NPC.Humanoid:MoveTo(part1.Position)"

1

Do you mean that, when the player is caught, the npc returns to part1, or goes to part4, then continues on to part5?

0

yeah similiar to that, if player caught npc returns part1 and start over

2 answers

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

7 comments

0

Movement code and Caught code 2 seprate part how can i implament this

0

btw whats mean (thank you golddog for interpreting) :D

0

this code goes in the first script. you will have to make a bindableevent and set Caught to it. in the second script, all you have to do is add bindableevent:Fire() when they get caught.

0

that thank you was for somebody in the comments, I had trouble understanding the question until I read it :) also, I have made some minor edits.

0

Okey i did bindableevent and put the code but i have an error says Players.xFurkanToDie.PlayerScripts.MoveToScript:49: attempt to index nil with 'Connect'

0

I did something it kinda works but npc choose parts completely random, you can check my answer.

0

btw when npc arrive the part1 or whatever part, npc going back to start

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.

5 comments

0

I suspected that would happen. that's because getchildren selects the waypoints semi-randomly. try to sort them alphabetically after you get them https://devforum.roblox.com/t/how-does-roblox-read-the-children-of-a-model-when-using-getchildren/205443/6. apologies for not mentioning it :)

0

even if we solve this problem if npc arrive the waypoint npc going back to reset

0

that's very strange. have you tried putting prints around to see where the problem is? also, I don't see you firing the event anywhere, do that first and it might fix the problem. I have edited my post.

0

Well i posted on devforum too, i found other solution here is the link: https://devforum.roblox.com/t/when-npc-catches-the-player-npc-movement-code-should-be-start-over/1179295/28 Thank you so much for helping ! Btw i have a still issue but its about to cooldown, when player caught npc will wait 30 second, the same guy who solved my problem solved write some code for me but code

0

But code doesnt work for me, he gave me place in his place code working but in my place it doesnt working its strange

This discussion is preserved as it was. New answers, comments, and votes are closed.