Pets!

Have a pet companion right by your side everywhere you go!

by lnsertYourself

Author Avatar

In this tutorial I'm going to show you how to script pets. A lot of games have a pet system and it makes sense why. Because they are so fun and cute! And so today I will show you a basic pet system that gives you a pet when you join the game.

Getting our pet

The simplest way is to get a premade mesh from the toolbox. Open the toolbox and drop-down to the "Mesh" tab. You will see many meshes to choose from. I'm going to choose a dragon because dragons are cool 😎 So click on a mesh of your choice and it will be inserted into your workspace. Feel free to scale the mesh bigger or smaller to make it a suitable size for a pet.

img|70x45

Let's move the pet to ReplicatedStorage and we will be able to give everyone their pets from there :)

The Code

This is a Script in ServerScriptService. I hope I've provided enough comments to understand the logic of it all. Here you go :)

local runService = game:GetService("RunService")

local pet = game.ReplicatedStorage:WaitForChild("Dragon") -- Replace with your pet name
local offset = CFrame.new(2, 2, 2) -- relative location of our pet to our character

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
        local humanoid = character:WaitForChild("Humanoid")

        local newPet = pet:Clone() -- New pet
        newPet.CanCollide = false -- Optional collisions
        newPet.CFrame = humanoidRootPart.CFrame * offset
 -- CFrame multiplication is very useful in calculating relative positions and rotations

        -- BodyPosition to move the pet
        local bodyPosition = Instance.new("BodyPosition")
        bodyPosition.Parent = newPet

        -- BodyGyro to rotate the pet
        local bodyGyro = Instance.new("BodyGyro")
        -- Default Torque is (40000, 0, 40000). This means 0 Torque on the Y-Axis. So we change it like so:
        bodyGyro.MaxTorque = Vector3.new(400000, 400000, 400000)
        bodyGyro.Parent = newPet

        -- Parent to character so when the character dies, the pet will die as well.
        newPet.Parent = character

        -- Here we will update the BodyPosition and BodyGyro values
        local update = runService.Heartbeat:Connect(function()
            bodyPosition.Position = (humanoidRootPart.CFrame * offset).Position
            bodyGyro.CFrame = humanoidRootPart.CFrame
        end)

        -- And when the character dies, stop updating the pet's forces. 
        humanoid.Died:Connect(function()
            update:Disconnect()
        end)
    end)
end)

img|25x25

Extra!

For fun, I have created another script that gives you a random pet every time you respawn. I have uploaded it to Roblox as a free model. Try it out in your studio! 😊

https://www.roblox.com/library/5615104197/Pet-Randomizer

Pets can bring a new experience to your games. Leave a good rating and get the word out! 🌟 Thank you for reading and Goodnight

View in-game to comment, award, and more!