Enemy NPC keeps being flung

  1. What do you want to achieve? Keep it simple and clear!
    To make a boss fight where the NPC does not fling everywhere

  2. What is the issue? Include screenshots / videos if possible!
    This NPC keeps getting flung for no reason.
    https://www.youtube.com/watch?v=LbCrTfrklvk

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have looked for solutions but they were too complicated or didnt work.

1 Like

Is the NPC massless? If so then Roblox physics is going to fling it.
Are you moving your NPC using a Humanoid, rig, scripting, Pathfinding?

We need details, including copy/pasting your script here. Put 3 backticks (```) before and after it so it formats properly.

None of it is massless. Here is my script

local hrpOfNPC = npc:WaitForChild("HumanoidRootPart")
local humanoid = npc:WaitForChild("Humanoid")
local plrsHit = {}
local maxDistance = 30 -- i can change this any time i want, this is just for convenience
local stancedis = 40
local pursuitCooldown = 0.1
local stancing = false
local stancecooldown = 13
local stancechancemax = 10
local stancechancemin = 1
local stancechecktime = 2

npc.Humanoid.Touched:Connect(function(touch)
	if humanoid.Health >= 1 then
		local player = game.Players:GetPlayerFromCharacter(touch.Parent)
		if player and not plrsHit[player] then
			plrsHit[player] = true
			local attackAnim = humanoid:LoadAnimation(math.random(1, 2) == 1 and script.attackerstab1 or script.attackerstab2)
			attackAnim:Play()
			npc.Stab.PlaybackSpeed = math.random(0.8,1.3)
			npc.Stab:Play()
			touch.Parent.Humanoid:TakeDamage(10)
			task.wait(1)
			plrsHit[player] = false  
		end
	end
end)

local function getClosestPlayer()
	local plrs = game.Players:GetPlayers()
	local closestHRP
	local closestDistance = math.huge

	for _, plr in pairs(plrs) do
		if plr.Character and plr.Character:FindFirstChild("HumanoidRootPart") and plr.Character:FindFirstChild("Humanoid").Health > 0 then
			local hrp = plr.Character.HumanoidRootPart
			local distanceBetween = (hrpOfNPC.Position - hrp.Position).Magnitude

			if distanceBetween < closestDistance then
				closestHRP = hrp
				closestDistance = distanceBetween
			end
		end
	end
	return closestHRP
end

spawn(function()
	while true do
		local targetHRP = getClosestPlayer()
		if targetHRP and (hrpOfNPC.Position - targetHRP.Position).Magnitude <= stancedis then
			if not stancing then
			if math.random(stancechancemin, stancechancemax) == 1 then
				stancing = true
				print("Stancing!")
				humanoid.WalkSpeed = 0
				local high = Instance.new("Highlight", npc)
				high.OutlineTransparency = 1
				high.FillTransparency = 0.5
				high.FillColor = Color3.new(0.372549, 0, 0)
				local stance = humanoid:LoadAnimation(script.attackerblock1)
				stance:Play()
				script.Parent.stance:Play()
				task.wait(math.random(3, 5))
				high:Destroy()
				stancing = false
				humanoid.WalkSpeed = 30
				wait(math.random(3,5))
				humanoid.WalkSpeed = 15
				task.wait(stancecooldown)
			else
				print("Not Stancing...")
			end
			if stancing then
				repeat local a = Instance.new("BoolValue",npc)
					a.Name = "1"
					wait(1)
					a:Destroy()
					wait(0.1)
				until not stancing
			end
		end

		end
		task.wait(stancechecktime)
	end
end)

while task.wait(pursuitCooldown) do
	if not stancing then
		local targetHRP = getClosestPlayer()
		if targetHRP and (hrpOfNPC.Position - targetHRP.Position).Magnitude <= maxDistance then
			npc.Humanoid:MoveTo(targetHRP.Position)
		end
	end
end```

Maybe it has something to do with the animation when the player touches the npc. Can you try anchoring the npc’s Humanoid.RootPart before the animation plays?

This slightly helped but the problem is still here.