#pet following

3 messages · Page 1 of 1 (latest)

thorny zephyr
#

i was makign a script a pet following script the pet follows the player for sometime and then the pet is getting self destroyed
its parented into the player character

#
local xdistance = 0 --The distance that the pets maintain from the player
local ydistance = 0
local zdistance = 3

script.Parent.Equipped:Connect(function()
    local pet1 = game.ServerStorage.Bee:Clone()
    local char = script.Parent.Parent

    if char.Humanoid.Health <= 0 then return end

    pet1.Parent = char

    local p = Instance.new("IntValue", pet1)
    p.Name = "pet"

    pet1:PivotTo(char.HumanoidRootPart.CFrame * CFrame.new(xdistance, ydistance, zdistance) * CFrame.Angles(math.rad(0), math.rad(-90), math.rad(0)))

    for i, part in pairs(pet1:GetChildren()) do
        if part:IsA("BasePart") or part:IsA("UnionOperation") then
            if part == pet1.PrimaryPart then
                continue
            end
            local constraint = Instance.new("WeldConstraint")
            constraint.Part0 = pet1.PrimaryPart
            constraint.Part1 = part
            constraint.Parent = part
            part.Anchored = false
        end
    end

    game.ReplicatedStorage.PetMovement:FireAllClients(pet1, char)
end)
script.Parent.Unequipped:Connect(function()
    local char = script.Parent.Parent.Parent.Character
    for i,v in pairs(char:GetChildren()) do
        if v:FindFirstChild("pet") then
            v:Destroy()
        end
    end
end)
#

so the script is like when equipped a tool it follows the player and when not equipped it destroys it