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)
It looks like there are a couple of issues with your code that might be preventing the NPC from following the player correctly:
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.
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:
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 .
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.
That’s what I told him 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.