I’m making a simulator game as you can guess I made a pet follow system as a prototype. I didn’t finish it yet. Not sure if it’s the best way to do it. That’s why I’m here. How can i improve this code?
Server:
local hum = char:WaitForChild("Humanoid")
local pets = {}
for i = 1, 5 do
local pet = Instance.new("Part")
pet.Anchored = false
pet.CanCollide = false
pet.Massless = true
pet.Size = Vector3.new(1, 1, 1)
pet.CFrame = hrp.CFrame
local owner = Instance.new("StringValue")
owner.Name = "Owner"
owner.Parent = pet
owner.Value = plr.Name
local order = Instance.new("NumberValue")
order.Name = "Order"
order.Value = i
order.Parent = pet
pet.Parent = workspace.PetFolder
pet:SetNetworkOwner(plr)
table.insert(pets, pet)
end
hum.Died:Connect(function()
for i,v in pairs(pets) do
v:Destroy()
end
end)
Client:
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hrp = char:WaitForChild("HumanoidRootPart")
local hum = char:WaitForChild("Humanoid")
local floatInc = 0.025
local maxFloat = 2
local minFloat = 0
local float = 0
local increase = true
workspace.PetFolder.ChildAdded:Connect(function(child)
repeat wait() until child:FindFirstChild("Owner") and child:FindFirstChild("Order")
if child.Owner.Value == plr.Name then
local pet = child
local moveLeft = child.Order.Value * 2.5
local Attachment1 = Instance.new("Attachment", hrp)
local Attachment0 = Instance.new("Attachment", pet)
Attachment1.Position = Vector3.new(7.5-moveLeft, 2, 3)
local AlignPosition = Instance.new("AlignPosition", pet)
local AlignOrientation = Instance.new("AlignOrientation", pet)
AlignPosition.Attachment0 = Attachment0
AlignPosition.Attachment1 = Attachment1
AlignOrientation.Attachment0 = Attachment0
AlignOrientation.Attachment1 = Attachment1
while wait() do
if hum.Health <= 0 then
pet:Destroy()
break
end
if float <= minFloat then
increase = true
elseif float >= maxFloat then
increase = false
end
if increase then
float = float + floatInc
else
float = float - floatInc
end
Attachment1.Position = Vector3.new(Attachment1.Position.X, float, Attachment1.Position.Z)
end
end
end)