so basically i don’t know how to make the pet appear behind the player.
script:
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local char = hit.Parent
while wait() do
script.Parent.CFrame= char.Head.CFrame
end
end
end)
You are attempting to set a position to a CFrame. You cannot do that, either set a CFrame to a CFrame or a position to a position. Also you can just weld the pet to the player or use an attachment.
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local char = hit.Parent
while true do
local targCFr = char.Head.CFrame
script.Parent.CFrame= targCFr + (targCFr.LookVector * -3)
wait()
end
end
end)
I think this is what you’re looking for. Although I don’t really get why you would initialize this with a touched event with no debounce.
Will first of all, you are using the wrong function. Touching a part to have a pet follow you? Your first statement makes no sense in this case, you are trying to have a pet follow you. Why have an if statement that checks to see if a humanoid touched a part?
This code will not execute correctly tho… you are trying to have a pet follow you, but you are checking if a humanoid touched a part? I am still very confused on why you have this function.
Why do that though? If he is trying to get a pet to follow him, it would make no sense to do that.
function givePet (player)
pet.CanCollide = false -- Makes it so the pet can't collide with anything.
if player then
local character = player.Character -- Gets character aspect of player.
if character then
local humRootPart = character.HumanoidRootPart -- Gets root part of the humanoid.
local newPet = pet:Clone() -- Clones our existing pet.
newPet.Parent = character -- The parent is the character.
local bodyPos = Instance.new("BodyPosition", newPet) -- Creates a body position for the pet/
bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
local bodyGyro = Instance.new("BodyGyro", newPet) -- Creates the body gyro.
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)
game.Players.PlayerRemoving:Connect(function()
pet:Destroy()
end)
Here is the code I use for my pets. I have commented out most of it to explain it.
That’s not the error though, because the code doesn’t make sense when the player touches the part. It won’t cause anything, but errors in the output bar.
Because it’s a way to verify that the thing which touched is a player/npc (usually a player, not sure though). This is a pretty common practice to ensure random unanchored parts don’t touch the part and break stuff.
As I mentioned earlier though, I agree the setup using a touched event is random and unnecessary.