Hello, I am trying to make a pet follow the player, even after they die. This works, but for some reason, the runservice.Stepped that I am using takes about 15 loops before disconnecting.
I am basically checking if the humanoid’s health is less than or equal to 0, and if it is, to disconnect the Runservice and to run the function again.
Here 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
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 debounce = false
local stepped
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
stepped:Disconnect()
if petList[player] == "EmojiCool" then
if not debounce then
print("this should only print once") -- this prints about 15 times
givePetEmojiCool(player)
debounce = true
end
end
end
end)
end
end
end
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 debounce = false
local stepped
stepped = runService.Heartbeat: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
stepped:Disconnect()
if petList[player] == "EmojiCool" then
if not debounce then
print("this should only print once") -- this prints about 15 times
givePetEmojiCool(player)
debounce = true
end
end
end
end)
end
end
end
This shouldn’t work but it’s best to try it, since you’re working with the character as there is a if statement checking if the Health is over 0 you can try and see if RenderStepped works, it shouldn’t but maybe it will?
Heartbeat should def work I’m just not entirely sure why it isn’t working for you.