Hi there! I’m trying to make pet following system, and i want to optimize it like the parent client will get all functions in the localscript.
Everytime i try it, roblox makes cooldown around 30-40 seconds before bodypositions and boodygyro’s works as well.
Here is the script:
--script
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
wait(0.55)
local Pets = Instance.new("Folder", char)
Pets.Name = "Pets"
local event = game.ReplicatedStorage.AnimatePets
for i = 1,25 do
local CopyOfPart = script.Model1:Clone()
CopyOfPart.Name = i
local BodyPos = Instance.new("BodyPosition", CopyOfPart.PrimaryPart)
local BodyGyro = Instance.new("BodyGyro", CopyOfPart.PrimaryPart)
BodyGyro.D = 350
BodyGyro.P = 3000
BodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
BodyPos.D = 600
BodyPos.P = 16500
BodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
CopyOfPart.PrimaryPart.Anchored = false
CopyOfPart.Parent = Pets
event:InvokeClient(player, CopyOfPart)
wait()
end
end)
--LocalScript
local RDS = game:GetService("RunService")
local RS = game:GetService("ReplicatedStorage")
local Character = game.Players.LocalPlayer.Character
local UpdatedCounter = 0
local HumRootPart = Character.HumanoidRootPart
local function UnEquipPet(pet)
local Pet = Character.Pets:FindFirstChild(pet)
if Pet then Pet:Destroy() end
end
local function EquipPet(pet)
UpdatedCounter = UpdatedCounter+1
local current_upd_counter = UpdatedCounter
local NumOfPets = #Character.Pets:GetChildren()
local current_degrees = 0
local current_degrees_adding = 360/NumOfPets
local function getting_Zsizing()
local ZSize = 0
for i,v in pairs(Character.Pets:GetChildren()) do
ZSize = ZSize+(v.PrimaryPart.Size.Z/3.5)
end
if ZSize < 6.5 then
ZSize = 6.5
end
return ZSize
end
local function create_following(degrees, pet, IsEquipped)
local ParentPartWSCFrame = HumRootPart.CFrame * CFrame.Angles(0, math.rad(degrees), 0)
local ZSize = getting_Zsizing()
local x,y,z = ParentPartWSCFrame:ToWorldSpace(CFrame.new(ZSize,0,ZSize)):components()
local pet_pos, last_humanoid_pos = Vector3.new(HumRootPart.Position.X-x, 0, HumRootPart.Position.Z-z), HumRootPart.Position
if IsEquipped then
pet:SetPrimaryPartCFrame(CFrame.new(pet_pos+last_humanoid_pos-Vector3.new(pet.PrimaryPart.Size.X,0,pet.PrimaryPart.Size.Z)))
end
RDS.RenderStepped:Connect(function()
if UpdatedCounter == current_upd_counter and pet.Parent ~= nil then
if (Vector3.new(HumRootPart.Position.X,0,HumRootPart.Position.Z)-Vector3.new(last_humanoid_pos.X,0,last_humanoid_pos.Z)).magnitude >= 0.25 then
pet.PrimaryPart.BodyPosition.Position = HumRootPart.Position+pet_pos
local x,y,z = HumRootPart.CFrame:ToWorldSpace(CFrame.new(0,0,-ZSize*25)):components()
pet.PrimaryPart.BodyGyro.CFrame = CFrame.new(HumRootPart.Position+pet_pos, Vector3.new(x,y,z))
else
pet.PrimaryPart.BodyPosition.Position = HumRootPart.Position+pet_pos
pet.PrimaryPart.BodyGyro.CFrame = CFrame.new(HumRootPart.Position+pet_pos, HumRootPart.Position)
end
last_humanoid_pos = HumRootPart.Position
end
end)
end
for _, current_pet in pairs(Character.Pets:GetChildren()) do
if current_pet == pet then
create_following(current_degrees ,current_pet, true)
else
create_following(current_degrees, current_pet, false)
end
current_degrees = current_degrees+current_degrees_adding
wait()
end
end
RS.AnimatePets.OnClientInvoke = function(pet)
EquipPet(pet)
end
Video: Roblox DevForum - YouTube