Move NPC away from player not working with no errors

I’ve been trying to make a “women repellant” type of game, but I haven’t been able to get my scripts to work. I’ve inserted just a regular r15 rig, and it wouldn’t work. Logically, I think this should work.

NPC model:
image

Script that’s in the NPC model:

function checkHRP()
	local closest 
	local closestMagnitude = 30
	for _, player in pairs(game.Players:GetPlayers()) do
		if player.Character then
			local hrp = player.Character.LowerTorso
			local hrpMagnitude = hrp and (script.Parent.LowerTorso.Position - hrp.Position).Magnitude or math.huge
			if closestMagnitude >= hrpMagnitude then
				closestMagnitude = hrpMagnitude
				closest = hrp
			end
		end
	end
	return closest
end
local runAwayDistance = 50
while wait() do
	local torso = checkHRP()
	if torso then
		script.Parent.Humanoid:MoveTo((CFrame.new(script.Parent.LowerTorso.Position,torso.Position) * CFrame.new(0,0,runAwayDistance)).Position)
	end
end
2 Likes

Update: I still haven’t been able to get this to work, even after some research. I get no errors in the workspace. Is it the humanoid:MoveTo call that isn’t working?

This isn’t a solution but you should change “while wait() end” to “while true do task.wait() end”. The only reason it even works is because wait() returns a number but if it were to not return a number then your while loop would stop. Also, why aren’t you just getting the HumanoidRootPart of the character instead of the LowerTorso? If the player has a R6 character then the “hrpMagnitude” will always be math.huge

function checkHRP()
	local closest 
	local closestMagnitude = 30
	for index, player in pairs(game.Players:GetPlayers()) do
		--print("Player = ", index)
		if player.Character then
			local hrp = player.Character.HumanoidRootPart
			local hrpMagnitude = hrp and (script.Parent.HumanoidRootPart.Position - hrp.Position).Magnitude
			if closestMagnitude >= hrpMagnitude then
				closestMagnitude = hrpMagnitude
				closest = hrp
			end
		end
	end
	--warn(closest)
	return closest
end
local runAwayDistance = 50
while true do
	task.wait()
	local torso = checkHRP()
	if torso then
		--warn("tryna move!")
		script.Parent.Humanoid:MoveTo((CFrame.new(script.Parent.LowerTorso.Position,torso.Position) * CFrame.new(0,0,runAwayDistance)).Position)
	end
end

This code I slightly edited works for me. Terrible to run though, your servers would be 1 FPS with this script.

How do you check your errors? Is your player R15 too or just the Dummy? Is the Dummy anchored? Good luck with this mystery.

1 Like

Could you send the place file? It wouldn’t work for me.

Yes, sorry for the delay, here it is.

NPC_RIG_EvercreeperDistro.rbxl (39.5 KB)

Why are you using CFrame in :MoveTo()?