NPC keeps falling over when spawned in

I have looked at many other topic on the devforum and non of them worked. Ive spent the last 3-4 hours trying many different solutions. And ive gotten as far as this `local autocorrect = true

local autocorrect = true

function recalculateHipHeight(character)
	script.Parent.Parent:FindFirstChild("Humanoid").HipHeight = (0.5 * script.Parent.Parent:FindFirstChild("HumanoidRootPart").Size.Y) + script.Parent.Parent:FindFirstChild("Humanoid").HipHeight
end
function recalculateHipHeight2(character)
	script.Parent.Parent:FindFirstChild("Humanoid").HipHeight = script.Parent.Parent:FindFirstChild("LeftLeg").HipHeight.Size.Y + (0.5 * script.Parent.Parent:FindFirstChild("HumanoidRootPart").HipHeight.Size.Y) + script.Parent.Parent:FindFirstChild("Humanoid").HipHeight
end

function PlayerRandomizer()
	pcall(function()
		script.Parent.Parent:FindFirstChild("HumanoidRootPart").Anchored = true
	end)
	local ids = require(game.Workspace:FindFirstChild(script.Parent.Parent.Parent.Parent.Owner.Value).FriendUserID)
	if #ids == 0 then
		script.Parent.Parent:SetPrimaryPartCFrame(script.Parent.Parent.Parent.Parent.AISpawn.CFrame)
		return
	else
		local succses, errormess = pcall(function()

			local ChoosenID = ids[math.random(1,#ids)]
			local Name = game.Players:GetNameFromUserIdAsync(ChoosenID)
			local Description = game.Players:GetHumanoidDescriptionFromUserId(ChoosenID)
			script.Parent.Parent.Humanoid:ApplyDescription(Description)
			script.Parent.Parent:SetPrimaryPartCFrame(script.Parent.Parent.Parent.Parent.AISpawn.CFrame)
			pcall(function()
				script.Parent.Parent:FindFirstChild("HumanoidRootPart").Anchored = true
			end)
		end)
		if not succses then
			PlayerRandomizer()
		end

		local s, e = pcall(function()
			spawn(function()
				while wait() do
					
					script.Parent.Parent:FindFirstChild("Humanoid"):SetStateEnabled(Enum.HumanoidStateType.FallingDown, false)
					script.Parent.Parent:FindFirstChild("Humanoid"):SetStateEnabled(Enum.HumanoidStateType.Ragdoll, false)
					script.Parent.Parent:FindFirstChild("Humanoid"):SetStateEnabled(Enum.HumanoidStateType.GettingUp, true)
				end
			end)	
			spawn(function()
				while wait() do
					if autocorrect == true then
						script.Parent.Parent:FindFirstChild("HumanoidRootPart").Velocity = Vector3.new(0,1,0)
					end	
				end
			end)
		end)
		
		if not s then
			print(e)
		end
		
		for i,v in pairs(script.Parent.Parent:GetChildren()) do
			if v:IsA("BasePart") then
				v.CollisionGroupId = 1
			end
		end
		wait(1)
		local ss, ee = pcall(function()
			if script.Parent.Parent:FindFirstChild("LeftFoot") then
				recalculateHipHeight(script.Parent.Parent)
			else
				recalculateHipHeight2(script.Parent.Parent)
			end	
		end)
		
		if not ss then
			print(ee)
		end
		
		pcall(function()
			script.Parent.Parent:FindFirstChild("HumanoidRootPart").Anchored = false
			wait(1)
			autocorrect = false
		end)	
	end
end

PlayerRandomizer()





wait(20)
pcall(function()
	if script.Parent.Parent ~= nil then
		script.Parent.Parent:Destroy()
	end	
end)

Do you know any way i could prevent that from happening?

1 Like

A video of the problem would help.


There is collision groups so the NPC’s can not collide with each other

Why are you setting the hipheight? That’s not what this post is about if that’s what you’re referring to - it’s for finding the height from the HumanoidRootPart.Position to the ground.

When you’re setting the CFrame of the character, that’s when you’re supposed to be using the methods from the post linked above. You’d do something along the lines of Character:SetPrimaryPartCFrame(AISpawn.CFrame + Vector3.new(0, Height, 0)).

oh, ill try fixing that and see if it works.

Edit: After changing that to actually recalculate the hipheight they still fall through the ground sometimes

Im not trying to position it since the AISpawn is already positioned properly for the NPC to be teleported to. Im trying to recalculate the hip height so the NPC legs don’t fall through the ground

The AISpawn is a part on the ground, right?

No its half way up in the air, I then have the velocity slowly lowering the NPC so they dont fall over

Your “recalculatehipheight” functions are still wrong, and possibly the problem here. Refer to my first post.

I know they were wrong which is why i changed them to this

function recalculateHipHeight()
	--if r15
	local character = script.Parent.Parent
	local bottomOfHumanoidRootPart = character.HumanoidRootPart.Position.Y - (1/2 * character.HumanoidRootPart.Size.Y)
	local bottomOfFoot = character.LeftFoot.Position.Y - (1/2 * character.LeftFoot.Size.Y)
	local newHipHeight = bottomOfHumanoidRootPart - bottomOfFoot

	local humanoid = character:FindFirstChildOfClass("Humanoid")
	humanoid.HipHeight = newHipHeight
end
function recalculateHipHeight2()
	--if r6
	script.Parent.Parent:FindFirstChild("Humanoid").HipHeight = 0
end

Make sure the AISpawn part’s Orientation is (0, 0, 0), and try again. It might be that you’re trying to tilt the humanoids when you’re setting the new CFrames, which humanoids don’t like.

The 4 on the left side are (0,0,0) and the 4 on the right side are (0,180,0) so it orientates the NPC’s CFrame towards the players plot

This really just looks like a hipheight issue. Why don’t you use the normal values?

Because i create a table of the players friends user id’s then i randomly choose one and load them in and the hip height is different for each

Can you give me some example IDs to work with and the exact code you’re using after fetching them?

local Players = game:GetService("Players")

local USERNAME = script.Parent.Name

local function iterPageItems(pages)
	return coroutine.wrap(function()
		local pagenum = 1
		while true do
			for _, item in ipairs(pages:GetCurrentPage()) do
				coroutine.yield(item, pagenum)
			end
			if pages.IsFinished then
				break
			end
			pages:AdvanceToNextPageAsync()
			pagenum = pagenum + 1
		end
	end)
end

local userId = Players:GetUserIdFromNameAsync(USERNAME)

local friendPages = Players:GetFriendsAsync(userId)

local usernames = {}
for item, pageNo in iterPageItems(friendPages) do
	table.insert(usernames, item.Username)
end

for i,v in pairs(usernames) do
	local playerid = require(script.Parent.FriendUserID)	
	print(Players:GetUserIdFromNameAsync(tostring(v)))
	table.insert(playerid, Players:GetUserIdFromNameAsync(tostring(v)))
end
local playerid = require(script.Parent.FriendUserID)

This fetches the id then puts them in a modulescript named FriendUserID which is then required from the script above

Where are these parented and how is it set up? Your code doesn’t make sense unless the 1st script is parented to the NPCs.

That first script shown is parented to the NPC, this script above is parented to the player

What happens if you add 1 to the HipHeight when you spawn them in to see if it’s the problem or not?

Another issue might be the Density of the NPC Parts. I’m just guessing here, but I know that having Tools or other items welded to the Player messes up their movements.

Ill check out setting the height to 1 and there isnt any parts welded other then their accessory’s. Maybe i should set the parts to massless?