-
What do you want to achieve? A simple pet following system
-
What is the issue?
while close to the center of the map, there is no issues, however when moving towards the end of the baseplate it starts becoming very choppy and I can’t figure out why. I’ll ammend this post with a video when I can. -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have tried changing the loops, but to no avail, I’ve tried unanchoring my pet, but that just broke everything, I’m a relatively new scripter so I’m at a loss for what I could’ve done wrong (Other than probably being super innefficient)
Local Script in StarterPlayerScripts
local runService = game:GetService("RunService")
local playerPets = workspace:WaitForChild("Player_Pets")
local circle = math.pi * 2
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character
if player.Character == nil then
player.CharacterAdded:Wait()
local character = player.Character
end
local folder = Instance.new("Folder")
folder.Name = game.Players.LocalPlayer.Name
folder.Parent = workspace:WaitForChild("Player_Pets")
wait(2)
local function getPosition(angle, radius)
local x = math.cos(angle) * radius
local z = math.sin(angle) * radius
return x, z
end
local Paramas = RaycastParams.new()
Paramas.FilterDescendantsInstances = {game.Workspace.Player_Pets:GetDescendants(), game.Workspace:WaitForChild(player.Name)}
Paramas.FilterType = Enum.RaycastFilterType.Exclude
Paramas.IgnoreWater = true;
function GetY(pos, size)
if workspace:Raycast(pos + Vector3.new(0,15,0),Vector3.new(0,-100,0),Paramas) then
return (workspace:Raycast(pos + Vector3.new(0,15,0),Vector3.new(0,-100,0),Paramas).Position.Y - player.Character:WaitForChild("HumanoidRootPart").Position.Y) + size.Y
else
return character.PrimaryPart.Position.Y
end
end
local function positionPets(character, folder)
for i, pet in pairs(folder:GetChildren()) do
local radius = 5
local angle = i + (circle / #folder:GetChildren())
local x, z = getPosition(angle, radius)
local sin = (math.sin(15 * time() + 1.6)/.5)+1
local cos = math.cos(7 * time() + 1)/4
local offsetY = - character.PrimaryPart.Position.Y
local petData = require(game.ReplicatedStorage:FindFirstChild("PetModels"):FindFirstChild(pet.Name):FindFirstChild("petData"))
local function lerpCharacter(moving)
if petData["flys"] == false then
if moving then
pet.PrimaryPart.CFrame = pet.PrimaryPart.CFrame:Lerp(character.PrimaryPart.CFrame * CFrame.new(x, GetY(pet.PrimaryPart.Position, pet.PrimaryPart.Size)+sin+1.5, z), 0.1)
else
pet.PrimaryPart.CFrame = pet.PrimaryPart.CFrame:Lerp(character.PrimaryPart.CFrame * CFrame.new(x, GetY(pet.PrimaryPart.Position, pet.PrimaryPart.Size)+2, z), 0.1)
end
else
pet.PrimaryPart.CFrame = pet.PrimaryPart.CFrame:Lerp(character.PrimaryPart.CFrame * CFrame.new(x, GetY(pet.PrimaryPart.Position)/2+math.sin(time()*3)+1, z), 0.1)
end
end
if character.Humanoid.MoveDirection.Magnitude > 0 then
lerpCharacter(true)
else
lerpCharacter(false)
end
end
end
game:GetService("RunService").Heartbeat:Connect(function()
for _, PlrFolder in pairs(playerPets:GetChildren()) do
local Player = game.Players:FindFirstChild(PlrFolder.Name) or nil
if Player ~= nil then
local character = Player.Character or nil
if character ~= nil then
positionPets(character, PlrFolder)
end
end
end
end)
local function createFolder()
if not workspace:WaitForChild("Player_Pets"):WaitForChild(Players.LocalPlayer.Name) then
local newFolder = Instance.new("Folder", workspace:WaitForChild("Player_Pets"))
newFolder.Name = Players.LocalPlayer.Name
end
end
game:GetService("UserInputService").InputBegan:Connect(function(input, GameProcessed)
if GameProcessed then
return
end
if input.KeyCode==Enum.KeyCode.X then
local pet = game.ReplicatedStorage.PetModels:WaitForChild("PetTest"):Clone()
createFolder()
pet.Parent = workspace:WaitForChild("Player_Pets"):WaitForChild(Players.LocalPlayer.Name)
end
end)
script in server script service
local Players = game:GetService("Players")
local PlayerPets = game.Workspace.Player_Pets
local function PlayerAdded(player)
local folder = Instance.new("Folder")
folder.Name = player.Name
folder.Parent = PlayerPets
end
local function PlayerRemoving(player)
local folder = PlayerPets:FindFirstChild(player.Name)
if folder == nil then return end
folder:Destroy()
end
for _, player in Players:GetChildren() do
task.spawn(function()
PlayerAdded(player)
end)
end
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)
Thanks in advance!!