How can I fix this script?

Hello, recently I have been making a pet that follows the player with a little floating animation but I can’t make it less brutal when going down here is what I mean:


It goes up slowly but when it’s going down it goes down really fast how do I fix this? Here is my code:

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        wait(3)
        local pet = game.ReplicatedStorage.Pet:Clone()
        pet.Parent = workspace
        local bg = Instance.new('BodyGyro', pet)
        local bp = Instance.new('BodyPosition', pet)
        local Drift = .08
        game:GetService('RunService').Heartbeat:Connect(function()
            Drift += .01
            if Drift >= 2 then Drift = .08 end
            local pos = char.HumanoidRootPart.CFrame + Vector3.new(4,Drift,4)
            bg.CFrame = char.HumanoidRootPart.CFrame
            bp.Position = Vector3.new(pos.x,pos.y,pos.z)
        end)
    end)
end)

Thank you!

I think its because your setting drift to .08 after it reaches two, which is would be weird since it would go down to .08 instantly rather than in a smooth motion, try implementing Drift -= 0.1 maybe

if Drift >= 2 then Drift -= 0.1 end
if Drift <= 0.9 then Drift += 0.1 end

You could try this out, it may work.

2 Likes

I recommend using TweenService for this. It does with that kind of stuff so, so much more accurately.

You can also find some tutorials on YouTube, as it’s not that hard to understand and stuff.

1 Like

Here is what happens when I use tweenService:


Here is my code:

local ts = game:GetService('TweenService')
local petfloatTween = TweenInfo.new(
    .5,
    Enum.EasingStyle.Sine,
    Enum.EasingDirection.InOut,
    -1,
    true,
    0
)

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        wait(2)
        local pet = game.ReplicatedStorage.Pet:Clone()
        pet.Parent = workspace
        local bg = Instance.new('BodyGyro', pet)
        bg.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
        local bp = Instance.new('BodyPosition', pet)
        bp.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
        bp.D = 700
        local Drift = pet:FindFirstChild('Float')
        game:GetService('RunService').Heartbeat:Connect(function()
            local upTween = ts:Create(Drift, petfloatTween, {Value = 3})
            upTween:Play()
            local pos = char.HumanoidRootPart.CFrame + Vector3.new(4,Drift.Value,4)
            bg.CFrame = char.HumanoidRootPart.CFrame
            bp.Position = Vector3.new(pos.x,pos.y,pos.z)
        end)
    end)
end)

EDIT: Float is a number value inside of the pet.

You’re supposed to change the properties from the BodyPosition using TweenService, not that “Drift” value.

Also have some sort of debounce or something for the runService thing.

1 Like

what is the problem? it is working fine

1 Like

Still not working:

local ts = game:GetService('TweenService')
local petfloatTween = TweenInfo.new(
    .5,
    Enum.EasingStyle.Sine,
    Enum.EasingDirection.InOut,
    -1,
    true,
    0
)

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        wait(2)
        local pet = game.ReplicatedStorage.Pet:Clone()
        pet.Parent = workspace
        local bg = Instance.new('BodyGyro', pet)
        bg.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
        local bp = Instance.new('BodyPosition', pet)
        bp.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
        bp.D = 700
        game:GetService('RunService').Heartbeat:Connect(function()
            local upTween = ts:Create(pet.Pos, petfloatTween, {Value = Vector3.new(4,8,4)})
            upTween:Play()
            local pos = char.HumanoidRootPart.CFrame + pet.Pos.Value
            bg.CFrame = char.HumanoidRootPart.CFrame
            bp.Position = Vector3.new(pos.x,pos.y,pos.z)
        end)
    end)
end)

Pos is a Vector3 value inside of the pet.

You’re supposed to change the BodyPosition/BodyGyro values and not directly the pets value, and here:

This wouldn’t be needed. (I think?) You should have a loop for handling going up and down and one heartbeat or something to handle player position etc.

Thank you! It worked perfectly!

1 Like