So I am trying to use someone’s following method and there is a problem that when humanoid dies, the pets don’t come back
local Character = script.Parent
local Humanoid = Character:FindFirstChild("Humanoid")
local Head = Character:FindFirstChild("Head")
local Torso = Character:FindFirstChild("HumanoidRootPart")
local maxFloat = 2
local floatInc = 0.1
local sw = false
local fl = 0
local function newInitialize()
while true do
wait()
if not sw then
fl = fl + floatInc
if fl >= maxFloat then
sw = true
end
else
fl = fl - floatInc
if fl <=-maxFloat then
sw = false
end
end
if Humanoid ~= nil and Head ~= nil then
if Humanoid.Health >= 1 then
coroutine.wrap(function()
local petArray = script.Parent:WaitForChild("Pets"):GetChildren()
for petIndex, petObject in pairs(petArray) do
local petRotationDelta = petIndex/#petArray
petObject.PrimaryPart.BodyPosition.Position = (
Torso.CFrame
* CFrame.Angles(0, petRotationDelta * 2 * math.pi, 0)
* CFrame.new(0, fl, 6)
).p
petObject.PrimaryPart.BodyGyro.CFrame = CFrame.new(
petObject.PrimaryPart.Position,
Torso.Velocity.Magnitude > 0 and petObject.PrimaryPart.Position + Torso.CFrame.LookVector or Torso.Position
)
end
end)()
end
end
end
end
local newInit
script.Parent:WaitForChild("Pets").ChildAdded:Connect(function()
if newInit then
coroutine.yield(newInit)
end
newInit = coroutine.create(newInitialize)
coroutine.resume(newInit)
end)
script.Parent:WaitForChild("Pets").ChildRemoved:Connect(function()
if newInit then
coroutine.yield(newInit)
end
newInit = coroutine.create(newInitialize)
coroutine.resume(newInit)
end)
local Character = script.Parent
local Humanoid = Character:FindFirstChild("Humanoid")
local Head = Character:FindFirstChild("Head")
local Torso = Character:FindFirstChild("HumanoidRootPart")
local maxFloat = 2
local floatInc = 0.1
local sw = false
local fl = 0
local function newInitialize()
while true do
wait()
if not sw then
fl = fl + floatInc
if fl >= maxFloat then
sw = true
end
else
fl = fl - floatInc
if fl <=-maxFloat then
sw = false
end
end
if Humanoid ~= nil and Head ~= nil then
if Humanoid.Health >= 1 then
coroutine.wrap(function()
local petArray = script.Parent:WaitForChild("Pets"):GetChildren()
for petIndex, petObject in pairs(petArray) do
local petRotationDelta = petIndex/#petArray
petObject.PrimaryPart.BodyPosition.Position = (
Torso.CFrame
* CFrame.Angles(0, petRotationDelta * 2 * math.pi, 0)
* CFrame.new(0, fl, 6)
).p
petObject.PrimaryPart.BodyGyro.CFrame = CFrame.new(
petObject.PrimaryPart.Position,
Torso.Velocity.Magnitude > 0 and petObject.PrimaryPart.Position + Torso.CFrame.LookVector or Torso.Position
)
local HumanoidRootPart = script.Parent:WaitForChild("HumanoidRootPart")
Humanoid.Died:Connect(function()
wait(10)
petObject.CFrame = HumanoidRootPart.CFrame
end)
end
end)()
end
end
end
end
local newInit
script.Parent:WaitForChild("Pets").ChildAdded:Connect(function()
if newInit then
coroutine.yield(newInit)
end
newInit = coroutine.create(newInitialize)
coroutine.resume(newInit)
end)
script.Parent:WaitForChild("Pets").ChildRemoved:Connect(function()
if newInit then
coroutine.yield(newInit)
end
newInit = coroutine.create(newInitialize)
coroutine.resume(newInit)
end)
What I do is have a folder in the player’s character called CharacterPets. All pets are put in here. Then, I have this code for putting pets back in after dying/respawning:
local function make_char_stuff(new_char)
local playerCharacterPets = dataModule:MakeInstance("Folder", "CharacterPets", new_char)
for _, p in pairs(plr:WaitForChild("Pets"):GetChildren()) do
if p.Equipped.Value then
dataModule:MakePetBody(plr, p.Name, p.ID.Value)
end
end
--More code down here for other stuff unrelated to pets
end
local char = plr.Character or plr.CharacterAdded:Wait()
make_char_stuff(char)
plr.CharacterAdded:Connect(make_char_stuff)
Also, just as a side note, do pet movement on the client side and make the pets on the server side. On the server side also make sure to set the network owner.