How To Fix This Recursive Function?

Hello, I am trying to make a pet that reappears after a player dies, or resets. But there is a problem with it.
The pet gets duplicated over and over.

This is my code


function givePetEmojiCool(player)
	if player then
		local character = player.Character
		if character then
			print("adding pet emojicool")
			local humRootPart = character.HumanoidRootPart
			local newPet = CoolEmojiPet:Clone()
			newPet.Name = "CoolEmojiTemplate"
			newPet.Parent = game.Workspace

			local head = newPet.HeadEmoji
			head.CFrame = humRootPart.CFrame * CFrame.new(10, 10, 10)
			local heliCopter = newPet.HeliCopter


			head.Anchored = false

			head:SetNetworkOwner(player)


			newPet.Parent = character

			--remoteEventY:FireClient(player, heliCopter.Name)

			local bodyPos = Instance.new("BodyPosition", head)
			bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			bodyPos.P = 10000

			local bodyGyro = Instance.new("BodyGyro", head)
			bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
			
			petList[player] = "EmojiCool"
			
			local rv = bodyGyro.CFrame.rightVector
			
			local stepped
			local humanoid = character.Humanoid
			local humanoidDied = false
			
			stepped = runService.Stepped:Connect(function()
				if character.Humanoid.Health > 0 then

					heliCopter.Orientation = Vector3.new(0, heliCopter.Orientation.Y - 2.5, 0)
					bodyPos.Position = humRootPart.Position + Vector3.new(2, 3, -6)
					bodyGyro.CFrame = humRootPart.CFrame * CFrame.Angles(0, math.rad(90), 0)
				else
					

					if petList[player] == "EmojiCool" then
						humanoidDied = true
					end
					stepped:Disconnect()
				end
			end)
			
			while humanoidDied == false do wait() end
		
			if humanoidDied then
				humanoidDied = false
				local char = player.Character or player.CharacterAdded:Wait()
				local player = game.Players:GetPlayerFromCharacter(char)
			
				givePetEmojiCool(player) -- this makes it recursive
				
			end
			
		end
	end
end

It has to do with the function calling itself over and over, but I have no idea how to fix it. I tried changing the humanoid’s health after they died, but that didn’t work either.

How could I fix this?

Thanks :slight_smile:

I realized the video has very slow fps, I will upload it again.

if petList[player] == "EmojiCool" then
    humanoidDied = true
end

I think the problem may lie in this section of the code, why are you setting humanoidDied as true if “EmojiCool” matches with whatever petList[Player] returns; humanoidDied should only be set to true if the player has in fact died, not in any other occasion.

Also why are you constantly checking for the humanoids health? Instead, why not use Humanoid.Died event or Player.CharacterAdded to trigger the function?

1 Like

I have no idea why I used that, I did it at night and so it wasn’t very efficient.
I removed and used humanoid.Died, but this time, it seems like the pet reappears on the old humanoid. And so, when the player’s character comes back, the pet is not there anymore.

function givePetEmojiCool(player)
	if player then
		local character = player.Character
		if character then
			print("adding pet emojicool")
			local humRootPart = character.HumanoidRootPart
			local newPet = CoolEmojiPet:Clone()
			newPet.Name = "CoolEmojiTemplate"
			newPet.Parent = game.Workspace

			local head = newPet.HeadEmoji
			head.CFrame = humRootPart.CFrame * CFrame.new(10, 10, 10)
			local heliCopter = newPet.HeliCopter


			head.Anchored = false

			head:SetNetworkOwner(player)


			newPet.Parent = character

			--remoteEventY:FireClient(player, heliCopter.Name)

			local bodyPos = Instance.new("BodyPosition", head)
			bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			bodyPos.P = 10000

			local bodyGyro = Instance.new("BodyGyro", head)
			bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
			
			petList[player] = "EmojiCool"
			
			local rv = bodyGyro.CFrame.rightVector
			
			local stepped
			local humanoid = character.Humanoid
			
			stepped = runService.Stepped:Connect(function()
				

				heliCopter.Orientation = Vector3.new(0, heliCopter.Orientation.Y - 2.5, 0)
				bodyPos.Position = humRootPart.Position + Vector3.new(2, 3, -6)
				bodyGyro.CFrame = humRootPart.CFrame * CFrame.Angles(0, math.rad(90), 0)
		
				
			end)
			
			humanoid.Died:Connect(function()
				stepped:Disconnect()
				local char = player.Character or player.CharacterAdded:Wait()
				local player = game.Players:GetPlayerFromCharacter(char)
				givePetEmojiCool(player)
			end)
		
		end
	end
end

Yeah cause Died triggers the moment the humanoid dies, so anything you create inside the player after the event will just get removed as the new characters gets respawned, so instead use Player.CharacterAdded which will allow a new pet to be instanced inside the player after it has recovered from death.

1 Like

That works, but I was just wondering if the humanoid.Died event would get duplicated lots of times, and if it could cause some lag.

According to the event’s page it should only trigger once, unless you purposely do something to trigger the died state multiple times.

1 Like