HumanoidRootPart breaks NPC when being teleported

So I just finished fixing a problem with my NPCs getting teleported to the same place (more on that on this devforum post, when I noticed that all of my zombie NPCs are broken upon being teleported to their random location!

Essentially what happens after they get teleported, is that their HumanoidRootPart gets offset a little bit from where they actually are, and causes them to flip upside down and not be able to move after being teleported!

Here’s what it looks like:

Here’s the script


local Template = script.Parent:FindFirstChildWhichIsA("Model")

local PP = Template.HumanoidRootPart

PP.Anchored = true

local x = PP.Position.X
local y = PP.Position.Y
local z = PP.Position.Z

local minx = x - 500
local maxx = x + 500

local minz = z - 750
local maxz = z + 750

local rolls = 0
local maxrolls = 100

local isStudio = false

local RunService = game:GetService("RunService")

if RunService:IsStudio() then
	
	isStudio = true
	
end

local studiomaxrolls = 5

if isStudio == true then

	while true do 

		if rolls >= studiomaxrolls then

			Template:Destroy()

			script:Destroy()

		end
		
		rolls = rolls + 1
		
		math.randomseed(rolls)
		local ranx = math.random()*(maxx-minx) + minx
		local ranz = math.random()*(maxz-minz) + minz
		
		print (ranx)
		print (ranz)

		local Clone = Template:Clone()
		Clone.Parent = script.Parent
		Clone:SetPrimaryPartCFrame(CFrame.new(ranx, y, ranz))
		Clone.HumanoidRootPart.Anchored = false
		
	end
end

I would like to know if this is a me problem or an engine problem, and how to fix it so that the HumanoidRootPart is “in sync” with the rest of the body when being teleported.

Here’s some more information:

  • the NPC still gets animated
  • CTRL+R (rotating on x+z axis) the NPC does nothing besides rotating the NPC 90 degrees (duh)
  • CTRL+T (rotating on y+ some other axis idk) the NPC causes it to glitch out
  • The HumanoidRootPart sometimes falls into the void or chases the player by itself (there’s a script inside of the enemy telling it to chase the player as a WHOLE enemy and not just a RootPart)
2 Likes
Clone.HumanoidRootPart.Anchored = false

maybe this is breaking him?
also try to change primary part

I don’t think that’s it because I added that after I started noticing their HumanoidRootPart bug out as a means of unbugging it

wait, is this server or local script
if server script, i have no idea what is happening, is not good day for programing
If no, then try server script

1 Like

It’s a server script, so it’s pretty strange… :confused: :confused: :confused:

1 Like

is not good day for programing

2 Likes

Perhaps you could try re-welding all the parts back to the HumanoidRootPart after the teleportation? Let me know what happens then.

1 Like

Try using Clone:MoveTo(Vector3.new(ranx, y, ranz))

1 Like

The NPC is still messed up after replacing :Cframe.New to :MoveTo, also isn’t :MoveTo just supposed to make them walk to a location? Because for some reason they teleported and buggered themselves up… Again :roll_eyes: :roll_eyes: :roll_eyes:

image

How would I go about with that though?

On second thought, I don’t think it would work. Welds would simply make the character unable to move, and I’ve forgotten that they weld relative to their position, so they wouldn’t even bring the parts back together. My mistake.

Thinking about it harder, a possible solution is to move the HumanoidRootPart again to the torso of the model after teleporting?

I’ve done something similar to what you’re trying to do in the past, so I will take a look at that project and see if I can figure out a solution later.

(Also, how do you format your code in your posts? I don’t know how…)

1 Like

I see you’re teleporting it 5 times in a single frame, it might be that Roblox’s physics engine can’t keep up with the changes and spazzes out. See if adding a wait() in the while loop or only teleporting once works.

2 Likes

Right so after putting a 5 second wait after the CFrame gets moved, the problem is still occuring

Try adding a small number to the Y coordinate like y+0.5 (of the CFrame you teleport to), since it might also be possible that it teleports it slightly inside a part and it flips over because the physics engine is responding to the collision.

1 Like

@Dysche @TimeVector @aca1231231 @CoinTheKit

Okay okay so I found out the problem revolves around when the enemy is cloned, after being cloned the HumanoidRootPart gets broken. However I still don’t know why that’s happening

1 Like

Try adding a return after destroying the template and script, because the clone code afterwards is still being ran if I am correctly, so it might break. Not sure if script:Destroy() stops the rest of the code from executing.


Also, I see you’re anchoring the HRP at the start of the script. Try unanchoring it before the clone not after. (Or don’t anchor at at the start at all)

local Clone = Template:Clone()
Clone.Parent = script.Parent
Clone:SetPrimaryPartCFrame(CFrame.new(ranx, y, ranz))
Clone.HumanoidRootPart.Anchored = false

to

PP.Anchored = false

local Clone = Template:Clone()
Clone.Parent = script.Parent
Clone:SetPrimaryPartCFrame(CFrame.new(ranx, y, ranz))

PP.Anchored = true
1 Like

Try doing this

Clone.HumanoidRootPart.CFrame = Clone.Torso.CFrame

That will make the CFrame match the torsos CFrame, so how it originally was

local Template = script.Parent:FindFirstChildWhichIsA("Model")

local PP = Template.HumanoidRootPart

local x = PP.Position.X
local y = PP.Position.Y
local z = PP.Position.Z

local minx = x - 500
local maxx = x + 500

local minz = z - 750
local maxz = z + 750

local rolls = 0
local maxrolls = 100

local isStudio = false

local RunService = game:GetService("RunService")

if RunService:IsStudio() then

   isStudio = true

end

local studiomaxrolls = 5

if isStudio == true then

    while true do 

	    if rolls >= studiomaxrolls then
		
		    Template:Destroy()
		
		    break

	    end

	    rolls = rolls + 1

	    math.randomseed(rolls)
	    local ranx = math.random()*(maxx-minx) + minx
	    local ranz = math.random()*(maxz-minz) + minz
	
	    local Clone = Template:Clone()
	    Clone.Parent = script.Parent
	    Clone.PrimaryPart = Clone.HumanoidRootPart
	    Clone:SetPrimaryPartCFrame(CFrame.new(ranx, y, ranz))
	
    end
end
 
script:Destroy()

Right, so I just tested this code right now and the HumanoidRootParts are in the correct position. I used the Anthro Drooling Zombie model (Marked “High Quality Item”, so no virus) from the toolbox as my test model. The zombies worked as intended and I experienced no problems with the script.

The changes I made were:

  1. Setting a primary part for the clones
  2. Changing the Script:Destroy() to a break; for some reason, the loop wasn’t ending properly if I used script:Destroy(). The clones would end up with all their parts gone except Humanoid. I’m assuming it’s because the template is destroyed but the loop still continues and the clones are thus rendered partless.
  3. Removing the anchoring and unachoring of the root part
  4. Adding a script:Destroy() at the very end because I assume you want to destroy the script after it is ran

Good luck, hope this works for you.

You don’t need to make a model have a primary part to teleport the humanoid. You might want to consider setting the HumanoidRootPart’s CFrame to the location you want.

Character.HumanoidRootPart.CFrame = CFrame.new(location)

HumanoidRootParts are special because they immediately go with the rest of the character’s model anyway, no matter where it moves.

I hope this helps your issue.

1 Like