Rig chasing script isn't working properly

I made a script for a Rig to chase players, but it only tracks them for a little bit and then stops moving, as shown in the video below.
The script works by checking if a player has entered a Part named TrackingRange to trigger the chase.
How can I fix or improve this?

(Since I’ve only recently started learning Luau and have been referencing scripts from various sources, the code might be a bit messy or hard to read…)

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Character = script.Parent
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local voiceA = Character.VoiceFolder:WaitForChild("Monster Roar (A)")
local voiceC = script.Parent.VoiceFolder:WaitForChild("Monster Roar (C)")
local voiceB = script.Parent.VoiceFolder:WaitForChild("Monster Roar (B)")
local TrackingRange = workspace.TrackingRange


HumanoidRootPart.Touched:Connect(function(hit)
	if not hit or not hit.Parent then return end
	if hit.Parent:FindFirstChild("Humanoid") then
		voiceB:Stop()
		voiceC:Stop()
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if math.random(1,2) == 1 then
			voiceA:Play()
		end
		humanoid.Health = humanoid.Health - 0.1
		task.wait(1)
	end
end)

local function checkPlayerInTrackingRange()
	local Tracking= false
	local result = workspace:GetPartsInPart(TrackingRange)
	for i,player in pairs(result) do
		if player.Parent:FindFirstChild("Humanoid") then
			Character.Humanoid:MoveTo(player.Parent.HumanoidRootPart.Position)
			Tracking = true
		end
		
	end
end

RunService.Heartbeat:Connect(checkPlayerInTrackingRange)

2 Likes

I think I may have found out what’s wrong with the script

Basically: in checkPlayerInTrackingRange, in these lines:

local result = workspace:GetPartsInPart(TrackingRange)
for i,player in pairs(result) do
	if player.Parent:FindFirstChild("Humanoid") then
		Character.Humanoid:MoveTo(player.Parent.HumanoidRootPart.Position)
	end
end

What is happening is you’re checking if player has a Humanoid instance inside of it; basically: checking if it’s a character or not in other terms

That’s pretty problematic in this case because of the fact that Character (which is the rig) is also inside of the TrackingRange, which gets it included into the result table as well

A fix for that would be to add an OverlapParams object as the 2nd parameter in :GetPartsInPart(TrackingRange) where OverlapParams.ExcludeInstances = {Character} (we want to exclude the rig from being included in the result table)

Here’s the fixed script:

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Character = script.Parent
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local voiceA = Character.VoiceFolder:WaitForChild("Monster Roar (A)")
local voiceC = script.Parent.VoiceFolder:WaitForChild("Monster Roar (C)")
local voiceB = script.Parent.VoiceFolder:WaitForChild("Monster Roar (B)")
local TrackingRange = workspace.TrackingRange

local overlap_params: OverlapParams = OverlapParams.new() -- changed
overlap_params.ExcludeInstances = {Character} -- changed

HumanoidRootPart.Touched:Connect(function(hit)
	if not hit or not hit.Parent then return end
	if hit.Parent:FindFirstChild("Humanoid") then
		voiceB:Stop()
		voiceC:Stop()
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if math.random(1,2) == 1 then
			voiceA:Play()
		end
		humanoid.Health = humanoid.Health - 0.1
		task.wait(1)
	end
end)

local function checkPlayerInTrackingRange()
	local Tracking= false
	local result = workspace:GetPartsInPart(TrackingRange, overlap_params) -- changed
	for i,player in pairs(result) do
		if player.Parent:FindFirstChild("Humanoid") then
			Character.Humanoid:MoveTo(player.Parent.HumanoidRootPart.Position)
			Tracking = true
		end

	end
end

RunService.Heartbeat:Connect(checkPlayerInTrackingRange)

In case you’re interested about OverlapParams, here’s some docs about them; they can be pretty useful in some boundary-querying cases like yours:

2 Likes

Thank you so much! I followed your advice, and it works perfectly smoothly now! I’m really grateful to have learned something new today. Thank you for the detailed explanation, and I will definitely check out the documentation! :grinning_face_with_smiling_eyes:

1 Like

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