Pet stops following after a few seconds of testing

Hi all! I’m using this code to make a ball follow overhead, but when testing the ball just randomly stops in midair roughly 10-40 seconds after starting. I’m not familiar with scripting, so this might be tripping something up and I’d love if someone could look it over. Thanks!

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) -- basically maximum speed and velocity is infinite

			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(0, 5, 0)
				bodyGyro.CFrame = humRootPart.CFrame
			end
		end
	end
end

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

I don’t know if this will help but try to change the while wait() do to:

task.spawn(function()
    while task.wait() do
           bodyPos.Position = humRootPart.Position + Vector3.new(0, 5, 0)
		   bodyGyro.CFrame = humRootPart.CFrame

the spawn function just runs other code below it instead of staying on the loop at all times

I used the whole script here but the pet didn’t appear at all : ( ty for assistance though

image

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) -- basically maximum speed and velocity is infinite

			local bodyGyro = Instance.new("BodyGyro", newPet)
			bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)

			task.spawn(function()
				while task.wait() do
					bodyPos.Position = humRootPart.Position + Vector3.new(0, 5, 0)
					bodyGyro.CFrame = humRootPart.CFrame
			end
		end
	end
end

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

Just trying random tbh but try this lol

game.Players.PlayerAdded:Connect(function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	
	if char then
		givePet(plr)
	end
end)

Sorry is this to replace the wait() or just standalone? :’)

Edit: Put it to replace the wait() and the pet still didn’t appear

Edit 2: That’s what I get for my ninja reply

Replace it for the bottom part of your code

The pet is back and follows successfully, but still freezes a few seconds into the test sadly.

image

Don’t use BodyPosition, it is deprecated. Use AlignPosition instead.

Hi I used AlignPosition but it seems to just lock the ball in place. I can see it moving and it moves my character when I touch it, but it just seems to lock here.

image

In the end I’ve scrapped this bit of code and I think it’s malfunction has to do with roblox deprecating BodyPosition. Going to try to figure something else out with AlignPosition.

Code:

local RunService = game:GetService("RunService")

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		InitPet(Player)
	end)
end)

function InitPet(Player)
	local PetReference = script.Parent
	
	if (Player) then
		local Character = Player.Character
		
		if (Character) then
			local HumanoidRootPart = Character.HumanoidRootPart
			local Pet1 = PetReference:Clone()
			
			local Attachment1 = Instance.new("Attachment", Pet1)
			local Attachment2 = Instance.new("Attachment", HumanoidRootPart)
			local AlignPosition1 = Instance.new("AlignPosition", Pet1)
			
			Attachment2.Position = Vector3.new(0, 5, 0)
			
			AlignPosition1.MaxForce = math.huge
			AlignPosition1.MaxVelocity = math.huge
			AlignPosition1.Responsiveness = 30
			AlignPosition1.Attachment0 = Attachment1
			AlignPosition1.Attachment1 = Attachment2
			
			Pet1.Parent = Character
		end
	end
end

Result:

Extra:
Remember to set the Collision of the “Pet” as false.

1 Like

Thank you so much! I was botching some horrid mishmash of code right when this notification came through. You’re a lifesaver.