Pet System BUG (MAJOR SCRIPTING)

1.So im working on a pet follow system for my game since I want to add pets and also have egg hatching systems and more included but im having an issue with the pet following system.

  1. So the issue is when I remove a pet from the players character the pet leaves a gap in the circle and when I add a pet the circle the gap never gets filled here is an example video.
    2023-08-28 10-27-48|video

  2. I have tried to find similar issues to others systems but those dont work so I came here wondering how I could do this so I came here to look for help as my attempts have failed multiple times. My script is down below.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local function setupCircularRotationAndBounce(character)
	local hrp = character:WaitForChild("HumanoidRootPart")
	local humanoid = character:WaitForChild("Humanoid")

	local parts = {}
	local numberOfParts = 8
	for _ = 1, numberOfParts do
		local NewDog = Instance.new("Model")
		local part = ReplicatedStorage.Dog:Clone()
		NewDog.PrimaryPart = part
		print(NewDog.PrimaryPart)
		NewDog.Name = part.Name
		part.Parent = NewDog
		part.Anchored = false
		part.CanCollide = false
		NewDog.Parent = script.Parent
		table.insert(parts, part)
	end

	local fullCircle = 2 * math.pi
	local radius = 16
	local bounceAmplitude = 0.2

	local function updateRadius()
		local currentTime = tick()
		local isWalking = humanoid.WalkSpeed > 0
		local isJumping = humanoid:GetState() == Enum.HumanoidStateType.Jumping

		local dogCount = 0
		for _, part in pairs(character:GetChildren()) do
			if  part.Name == "Dog" then
				dogCount = dogCount + 1
			end
		end

		radius = 10 + (dogCount * 0.5)  -- Adjust the multiplier for extending the circle

		for i, dogModel in pairs(character:GetChildren()) do
			if dogModel.Name == "Dog" and dogModel:IsA("Model") then
				local dogPart = dogModel.PrimaryPart
				if dogPart then
					local angle = (i * (fullCircle / #parts)) + ((currentTime * 0.5) % fullCircle)
					local x, z = math.cos(angle) * radius, math.sin(angle) * radius

					local targetPosition = hrp.Position + Vector3.new(x, 0, z)

					-- Add vertical bouncing if the player is walking or jumping
					if isWalking or isJumping then
						local bounceHeight = math.abs(math.sin(currentTime * 5)) * bounceAmplitude
						targetPosition = targetPosition + Vector3.new(0, bounceHeight, 0)
					end

					dogPart.Position = targetPosition
				end
			end
		end

	end

	game:GetService("RunService").Heartbeat:Connect(function()
		updateRadius()
	end)

	character.ChildAdded:Connect(function(child)
		if child.Name == "Dog" then
			updateRadius()
			table.insert(parts, child)
		end
	end)

	character.ChildRemoved:Connect(function(child)
		if child.Name == "Dog" then
			updateRadius()
			local indexToRemove
			for i, part in ipairs(parts) do
				if part == child then
					indexToRemove = i
					break
				end
			end
			if indexToRemove then
				table.remove(parts, indexToRemove)
			end
		end
	end)

	updateRadius() -- Initial update
end

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		setupCircularRotationAndBounce(character)
	end)
end)

for _, player in ipairs(Players:GetPlayers()) do
	if player.Character then
		setupCircularRotationAndBounce(player.Character)
	end
end

All help is appreciated as this has been very complicated to fix.

1 Like

All Help is appreciated as there is a lot of bugs

The main issue you’re encountering is likely related to how you’re updating the parts array when a pet is removed from the character. The parts array should always reflect the correct list of dog parts to position around the player. However, when a pet is removed, you’re removing the pet’s Model from the parts array, which can lead to inconsistencies.

Here’s an approach you can consider to fix the issue:

  1. Instead of directly manipulating the parts array, use a function to rebuild the array every time a pet is added or removed.
  2. When a pet is added, call this function to update the parts array.
  3. When a pet is removed, simply remove the pet’s Model from the circular arrangement without modifying the parts array.

Here’s a modified version of your code with these changes:

luaCopy code

-- ... (other code remains the same)

local function updatePartsArray(character)
    local parts = {}
    for _, child in pairs(character:GetChildren()) do
        if child.Name == "Dog" and child:IsA("Model") then
            table.insert(parts, child)
        end
    end
    return parts
end

character.ChildAdded:Connect(function(child)
    if child.Name == "Dog" then
        updateRadius()
    end
end)

character.ChildRemoved:Connect(function(child)
    if child.Name == "Dog" then
        updateRadius()
    end
end)

updatePartsArray(character) -- Initial setup

-- ...

-- Inside the updateRadius function
function updateRadius()
    local parts = updatePartsArray(character)
    -- The rest of your updateRadius logic
end

This way, the parts array will always be generated based on the current state of the character’s children with the name “Dog”, ensuring that the circular arrangement stays consistent when pets are added or removed.

Remember to keep track of the arrangement of pets and their positions in the circular arrangement. If you’re still facing issues, make sure to debug and print out relevant information to help identify the root cause of the problem.

1 Like

Can u give me the full revised code with this included

I’m sorry but he literally gave you the solution and you’re still asking for him to put it all together? He even put a part that said “-- … (other code remains the same)”.

Read the other comment I did implement the solution

I implemented this and it still did not work as needed

hm idk than i really think its something you have to fix and ask a major dev

1 Like

Nvm I figured it out by myself but thanks for the help

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.