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.

2 Likes

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.

Who were you replying to? (replying to yourself doesn’t give us notifications)

Oh, my bad. I was replying to critical gorilla

Just mention @CriticalGorilla like this in your post. They’ll get the notification.

Can you try commenting out the code for the animation? If the flinging still happens when the player touches the npc, then it’s not an issue with the animation.

Weird roblox physics. I’m not 100% why it happens but you could try doing one of the following:

  1. Make the boss super heavy by increasing the Density of the parts
  2. Make it so that the player can’t collide with the boss with CollisionGroups

It could also be an issue with animations like what CriticalGorilla said above

This didnt work. Im going to try thoms solutions now…

1 just flings the player instead and 2 would be a hassle because the player model is in a package across all the places…

Your Rigid Body for the arms / legs are intersecting at the points with other objects, causing the physics to fling the character. When your animation forces the mesh’s into another rigid body, it will perform that collision pushback.

It doesn’t matter how much mass or how your collision groups work (since you don’t want to change collision groups, but also the fact that the character will have to be non-collideable with everything anyways), it’s about how your mesh’s Rigid Body is created, which is automatically calculated by Roblox engine.

If you created the model, you’d have to review your mesh object in Blender, or make the mesh non-collideable and use a simpler mesh object to act as the Rigid Body / collider. Otherwise, you’ll have to rework your character that has simpler mesh collisions for roblox to calculate, whether that’s a new model or changing your collision fidelity properties.

1 Like

For now, the easiest solution is to try out a simple box / cube mesh that will act as the collider, because you’re unsure if it’s actually the mesh itself or the code. If it’s still flinging even when the character’s body is non-collideable and you have a hitbox as replacement, then that would point towards how you’re handling the movement of the character, rather than the mesh itself.

However, if it’s the other way, where the replacement did in-fact fix the fling, then you’d have to do the heavy lifting of redoing your meshes.

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