Is there a bug in my pet following script?

Hi guys,

So I scripted this part that follows you and everything, but there is a problem where there is supposed to be only one part that following the person but when you press play, there is two. If you dont get what I am saying here is a video robloxapp-20210228-1047017.wmv (1.4 MB) .

And this is before,

,

This is 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 = 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)

while wait() do
bodyPos.Position = humRootPart.Position + Vector3.new(2, 2, 3)
bodyGyro.CFrame = humRootPart.CFrame
end
end
end
end

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

Also it seems like the part just standing there shown in the video is not functional.

Any help is appreciated!

1 Like

When pasting code could you please use the Preformatted Text option? This will help to read the script easier.

It seems like you’re cloning the pet, which is already in workspace, so you’re going to get two of them. If you dont want to see the source, I’d suggest putting that into Replicated Storage, and use the script to move the close into the workspace.

2 Likes

Do you know how I can fix the script?

1 Like

To preformat text just place a
“```lua” at the start of your text, then end with “` ``” (no space)

-- ```lua
local Example = "See?" 
-- ```

Anyways, your script does exactly what you tell it to do, your “problem” is that the source part is visible.

What your script does is take a copy of the part, then make it follow the player, this leaves the original part where it is. Here is what I would suggest doing:

Rename the part to “Shrek” or whatever you choose.
Place the part you would like to follow the player into game>ReplicatedStorage
Move the script that’s underneath the part to game>ServerScriptService
Then delete line 1 (local pet = script.Parent) and replace it with

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local pet = ReplicatedStorage:FindFirstChild("Shrek") -- Replace "Shrek" with whatever you decide to name the part.
2 Likes

By the way, you can just write
“```”

“```”
without the “lua” and it will work.

1 Like