Npc following problem!

So I was trying to make an npc follow you but more like a pet but it doesnt work can someone help me heres the script. btw its an npc dummy.

local pet = script.Parent

function givePet (player)
	if player then
		local character = player.Character
		if character then
			local humRootPart = character.HumanoidRootPart
			local newPet = pet:Clone ()
			newPet.Parent = character

			local bodyPos = Instance.new("BodyPosition", newPet)
			bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

			local bodyGyro = Instance.new("BodyGyro", newPet)
			bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
                        newPet.CanCollide = false -- I removed this becouse its an npc dummy so it doesnt collide i did un collide all the body parts.
			while wait() do
				bodyPos.Position = humRootPart.Position + Vector3.new(4.5, -0.9, 1.5)
				bodyGyro.CFrame = humRootPart.CFrame
			end

		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		givePet(player)
	end)
end)

Can you tell me more about what’s not working? Are there any error messages or is the NPC just not following the player as expected?

its not doing anything thats the problem

Can you check the output and tell me if there’s an error?

It looks like there are a couple of issues with your code that might be preventing the NPC from following the player correctly:

  1. The newPet object is being parented to the player’s character, which means that it will move along with the character as the character moves. However, the position and orientation of the newPet object is also being controlled by the bodyPos and bodyGyro objects, which will cause the newPet object to move and rotate independently of the player’s character. This might cause the newPet object to move in unpredictable ways.
  2. The position and orientation of the newPet object are being updated in a while wait() do loop. This loop runs continuously, which might cause the script to slow down or crash if it’s run for too long.

To fix these issues, you could try modifying the script as follows:

  1. Instead of parenting the newPet object to the player’s character, you could parent it to the workspace so that it moves independently of the player’s character. To do this, change newPet.Parent = character to newPet.Parent = workspace .
  2. Instead of updating the position and orientation of the newPet object in a while wait() do loop, you could use a RunService.Heartbeat event to update the position and orientation of the newPet object once per frame. This should help to prevent the script from slowing down or crashing.

Here’s an example of how you could modify the script:

local pet = script.Parent

function givePet(player)
    if player then
        local character = player.Character
        if character then
            local humRootPart = character.HumanoidRootPart
            local newPet = pet:Clone()
            newPet.Parent = workspace

            local bodyPos = Instance.new("BodyPosition", newPet)
            bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

            local bodyGyro = Instance.new("BodyGyro", newPet)
            bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)

            game:GetService("RunService").Heartbeat:Connect(function()
                bodyPos.Position = humRootPart.Position + Vector3.new(4.5, -0.9, 1.5)
                bodyGyro.CFrame = humRootPart.CFrame
            end)
        end
    end
end

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        givePet(player)
    end)
end)

Note: Depending on the size and shape of your NPC dummy, you may need to adjust the position offset in the bodyPos.Position calculation to get the NPC to follow the player correctly.

Stop using ChatGPT, thanks :blush:. Have a nice day.

1 Like

Get good, you are just jealous.

1 Like

That’s what I told him :wink: Haha. I think that even using chatGPT is a help since not everyone gets the same answer. My chatGPT is broken bruh. I always have errors and false answers.

1 Like

Wait do you want it like jojo stands?

hey small reccomendation please use alligns and also setnetworkowner for the pet parts to nil.

Just flag his posts

i dont think flagging a kid’s posts a good idea, come on let him live his npc’ish live there.

1 Like

bruh_____________________________