Chase script troubles

(in the vid I hover over the output but u cant see it so dw about it)

local Target = nil
local activounce = false

local Humanoid = script.Parent:WaitForChild("Humanoid")

local chasing = false
local Character = script.Parent

local Range = 45
local Debug = false

local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local function Chase()
	if Target ~= nil then
		Humanoid:MoveTo(Target.Position)
	else
		return nil
	end
end
local IsMoving = false
local function Move()
	if IsMoving == false then
		IsMoving = true
		Humanoid:MoveTo(HumanoidRootPart.Position + Vector3.new(math.random(-25,25),0,math.random(-25,25)))
		wait(math.random(1,3))
		IsMoving = false
	end
end
script.Parent.abouttagetreal.OnServerEvent:Connect(function(player)
	if chasing == true then return end
	print("connect")
	local function ondeath()
		player.CharacterRemoving:Connect(function()
			chasing = false
		end)
	end
	task.spawn(ondeath)
	
	script.Parent.Head.ClickDetector.CursorIcon = "rbxassetid://1822114127"
	script.p2enter:Play()
	wait(0.2)
	script.Parent.Head.ClickDetector.CursorIcon = "rbxassetid://"
	script.chasesound:Play()
	print('why hello')
	chasing = true
	while chasing do
		print('hiyas')
			if player and player.Character == nil then
			chasing = false 
			script.chasesound:Stop()
			else
				print(""..player.Character.Name)
				local Char = player.Character
				if Char and Char:IsA("Model") then

					if Char:FindFirstChild("HumanoidRootPart") then
						local Root = Char:FindFirstChild("HumanoidRootPart")



						if Char ~= Character then
							if (HumanoidRootPart.Position - Root.Position).Magnitude <= Range then
								if Target == nil then
									Target = Root
								else
									
									repeat 
									wait(0.1)
										if chasing ~= false then
											
										Chase()
										else
										end
									until (chasing == false)
								print("omgyay")
								script.chasesound:Stop()
								
								-- do end stuff here 
								end
							elseif (HumanoidRootPart.Position - Root.Position).Magnitude > Range  and chasing == true then
								Move()
							end
						end
					end
				end
			wait()
		end
		wait()
		Debug = false
	end

end)




is my script. It is a server script that fires from a local script that fires every time perfectly. However, when I reset my character after activating the chase script once, the ai just doesn’t chase. I’ve tried breakpointing and finding the problem, but I just couldn’t find it. It even printed fine at the Chase function. The chase script was something I’d took off the toolbox, and then I just rescripted it to my needs, so I would say to look out for things or variables that just dont get changed and need to. The script has a load of if statements and despite me looking over all of them that just may be the problem? Im not quite sure. I don’t think this owuld be too hard to fix, its jsut that since I’m not on a high level of scripting yet, I can’t get ont he same pace with what other coders are trying to do sometimes; even if I do I can’t continue the script without forgetting somethign like a variable they were using in an if statement, making sure it’s left true for their if statement to be running (thats an example)
SOMETHING TO KEEP IN MIND! for some reason, in the output: the first time the event fires, the time it works, the prints print twice.. as if the script is running through itself two times from something like a return? in the second round I only see one print (this does indeed happen every time)
please help, all of it is greatly appreciated!

1 Like

i believe the problem lies in that youre not correctly updating the target variable so the npc breaks cuz its still tryna chase that non existant target. you also simply relied on the chasing variable for the npc, so i added some “safety” to minimize the chances of the npc freezing up. also js a few good practices - you should disconnect events when you’re done with them to prevent memory leaks (or you can use :Once like i did) and you should use task.wait instead of wait as wait is deprecated (but i understand you got this from the toolbox so it’s outdated)

tl;dr:
you dont correctly update the target variable and i just added some extra “safety measures” and fixed up some code

anyways, i think you should first try this code before reading allat in case it doesnt work :sweat_smile:

local Players = game:GetService("Players")

local Humanoid = script.Parent:WaitForChild("Humanoid")
local HumanoidRootPart = script.Parent:WaitForChild("HumanoidRootPart")

local Target = nil
local chasing = false
local Range = 45

local function Chase()
	if Target then
		Humanoid:MoveTo(Target.Position)
	end
end

local IsMoving = false
local function Wander()
	if not IsMoving then
		IsMoving = true
		Humanoid:MoveTo(
			HumanoidRootPart.Position + Vector3.new(math.random(-25, 25), 0, math.random(-25, 25))
		)
		task.wait(math.random(1, 3))
		IsMoving = false
	end
end

script.Parent.abouttagetreal.OnServerEvent:Connect(function(player)
	if chasing then return end

	print("Chase triggered")
	chasing = true
	Target = nil

	player.CharacterRemoving:Once(function()
		chasing = false
		Target = nil
		script.chasesound:Stop()
	end)

	while chasing and player and player.Character do
		local Char = player.Character
		local Root = Char:FindFirstChild("HumanoidRootPart")

		if Root and (HumanoidRootPart.Position - Root.Position).Magnitude <= Range then
			Target = Root
			Chase()
		else
			Target = nil
			Wander()
		end

		task.wait(0.1)
	end

	print("Chase ended")
end)

thanks! this worked. I learned a few.. had no idea abou the memory leakin and allat :sob: ill be sure to study the code later on

alg lol. memory leaking is essentially just pieces of code that could cause excessive and unwanted memory usage. even just small connections can build up. imagine if you have say 20 players in your game and they have triggered multiple chases over the course of a few hours. all of those player.CharacterRemoving connections are still running, meaning that youre listening for the same thing like 15 times. a more significant example is with connections for things like runservice. if you leave unnecessary renderstepped connections (which run every single frame!) it could all add up!

edit: lmk if you need help with understanding anything from the code

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.