Hey DevForum!
Recently I’ve been working on a simple pet follow module, where I can assign pets to a player with specific functions. However, I’ve run into an issue illustrated below:
The pets follow the player with a delay.
The pets use an AlignOrientation to rotate the pet (which has no issues) and an AlignPosition to make the pet follow the player (which takes its own sweet time to do so).
I’ve tried setting the network ownership of the pet to the player, no luck. I’ve looked up on the DevForum for any solutions but I couldn’t find any that could solve mine. I thought it’s because I change the Position of AlignPosition on the server using RunService.Heartbeat
, so I ran an local server and tested, but the delay occurs on the server too.
Here’s my code so far:
follow() function of ModuleScript in ReplicatedStorage.Modules
function pet:follow(distance, radius, hoverHeight)
distance = tonumber(distance) or 6;
radius = math.rad(tonumber(radius) or 0);
hoverHeight = tonumber(hoverHeight) or 4;
self._connections.follow = runService.Heartbeat:Connect(function()
local obj = self.object;
if not obj then return; end
if not self:updateState() then return; end
local character = self.owner.character;
local hrp = character.PrimaryPart;
if not hrp then return; end
--local posXZ = (hrp.CFrame * CFrame.fromEulerAnglesXYZ(0, radius, 0) * CFrame.new(0, 0, distance + (obj.Size.Z / 2))).Position;
local posXZ = hrp.Position + Vector3.new(math.cos(radius) * (distance + obj.Size.X / 2), 0, math.sin(radius) * (distance + obj.Size.Z / 2));
raycastParams.FilterDescendantsInstances = { character, folder };
local res = workspace:Raycast(posXZ + Vector3.new(0, 7.5, 0), Vector3.new(0, -20, 0), raycastParams);
local finalPosition = (res and res.Position) or posXZ - Vector3.new(0, 2.5, 0);
finalPosition += Vector3.new(0, hoverHeight + (obj.Size.Y / 2), 0);
if self.animate and self.state == "Idle" then
finalPosition += Vector3.new(0, math.sin(os.clock() * self.animMultiplier) * self.animOffset, 0);
end
obj.Gyro.Attachment0 = obj.Center;
obj.Move.Attachment0 = obj.Center;
--obj.Gyro.CFrame = CFrame.lookAt(obj.Position, character.Head.Position);
obj.Gyro.CFrame = hrp.CFrame;
obj.Move.Position = finalPosition;
end);
end
Testing Script in ServerScriptService
local playersService = game:GetService("Players");
local petModule = require(game:GetService("ReplicatedStorage").Modules.Pet);
playersService.PlayerAdded:Connect(function(player)
player.CharacterAppearanceLoaded:Wait();
for i = 0, 330, 30 do
local p = petModule.new("Pet", player);
p:follow(6, i, 4);
end
end);
Any help would be appreciated, thank you in advance!