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!
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)
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
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…)
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.
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.
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
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
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:
Setting a primary part for the clones
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.
Removing the anchoring and unachoring of the root part
Adding a script:Destroy() at the very end because I assume you want to destroy the script after it is ran
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.