NPC Pushing the Player Around?

Hi!
I’m making a game where, in some levels, an NPC follows you around. Its character model is cloned from storage locally and parented inside the actual player’s model. It’s set to walk towards a specific point relative to the player. However, for some reason, if that point is within a wall, and the player jumps, the player will be pushed away as though the NPC is trying to get to that point. But ONLY when jumping. I recorded it happening here:


It’s very odd. I found out that if I parent the NPC simply in just Workspace, and NOT under the player’s model, the problem is fixed, but none of the animations on the NPC work.

Does anyone have an idea on what might be causing this?

1 Like

Could you give me something to work with, like give me the children of the NPC, and player model?

Sure. Here’s everything within the player model:


The “Companion” model is the NPC.

Here’s everything within the HumanoidRootPart of the player, if at all important:
image
The “CharacterHitBox” and “HomingRadius” are bricks attached to it, and literally act as hitboxes. But they’re massless and non-collide-able, so they haven’t been causing any trouble. Just in case, I thought it might help to include it.

…and here’s what’s inside the NPC’s model:
image
The “LeftArmWeld,” “RightArmWeld,” etc. models are things welded to the body parts to act as clothes. They are also massless and non-collide-able. There’s nothing inside any of the legs, torso, etc. of the NPC that are out of the norm.

If it comes down to it, I’m also willing to make a repro file to make things easier.

Ok, now how do you load the animation, can you share your snippet of code?

For the NPC, the animation is loaded simply by having an animation script within the NPC already. It’s essentially just the default Roblox animation script, just with different animation IDs. It’s a pretty large script, and it’s hard to figure out what might be important in all of it. Here’s the very beginning of it that might be important:

function   waitForChild(parent, childName)
	local child = parent:findFirstChild(childName)
	if child then return child end
	while true do
		child = parent.ChildAdded:wait()
		if child.Name==childName then return child end
	end
end

local Figure = script.Parent
local Torso = waitForChild(Figure, "Torso")
local RightShoulder = waitForChild(Torso, "Right Shoulder")
local LeftShoulder = waitForChild(Torso, "Left Shoulder")
local RightHip = waitForChild(Torso, "Right Hip")
local LeftHip = waitForChild(Torso, "Left Hip")
local Neck = waitForChild(Torso, "Neck")
local Humanoid = waitForChild(Figure, "Humanoid")
local pose = "Standing"

local currentAnim = ""
local currentAnimTrack = nil
local currentAnimKeyframeHandler = nil
local currentAnimSpeed = 1.0
local oldAnimTrack = nil

However, since the animation breaks when the NPC is parented under just the Workspace, I feel like the script doesn’t actually do anything, and the NPC might be using the player’s animation script. Especially since scripts don’t run when something is cloned locally, right?
So, here’s how animations are loaded on the player: A server script within ServerScriptService clones the local animation script into the player’s model. Here’s the server script that does so:

function onPlayerRespawned(newPlayer)
	while true do
		if newPlayer.Character:findFirstChild("Animate")~=nil then break end
		wait()
	end
	newPlayer.Character.Animate.Disabled = true
	local c = script.Anima:Clone()
	c.Parent = newPlayer.Character
	c.Disabled = false
end

function onPlayerEntered(newPlayer)
	newPlayer.Changed:connect(function (property)
		if (property == "Character") then
			onPlayerRespawned(newPlayer)
		end
	end)
end

game.Players.PlayerAdded:connect(onPlayerEntered)  

…and the local script it clones is the same as the one that’s in the NPC.

1 Like

Hi!

Don’t parent the NPC to the player, parent it to the workspace instead.

Take the Anima script you have for the NPC, and the line of code where it defines Figure (assuming it’s Roblox’s default animation script), just set that to game.Workspace:WaitForChild("Companion").

Let me know if it works!

EDIT: So sorry! Take that Anima script and insert it into StarterCharacterScripts.

3 Likes