Hi I Making A Pet Movement But If Player Jump Pets Starting Flying.
local PetsFolder = Instance.new("Folder",workspace)
PetsFolder.Name = "Pets"
local RunService = game:GetService("RunService")
local fullCircle = 2 * math.pi
local radius = 10
local function getXAndZPositions(angle)
local x = math.cos(angle) * radius
local z = math.sin(angle) * radius
return x, z
end
local radius = 7
local AlignPosition_MaxForce = 25000
local AlignPosition_Responsiveness = 100
local AlignOrientation_MaxTorque = 25000
local AlignOrientation_Responsiveness = 100
game.ReplicatedStorage.PetActionEvent.OnServerEvent:Connect(function(plr,stat,char,petfolder)
if stat == "Move" then
if PetsFolder:FindFirstChild(plr.Name) and plr.Character then
local petsTable = {}
for i,v in pairs(PetsFolder[plr.Name]:GetChildren()) do
if v:IsA("Model") then
table.insert(petsTable,v)
end
end
for i,v in pairs(petsTable) do
if v.ClassName and v:IsA("Model") and v.PrimaryPart and v.PrimaryPart:IsA("BasePart") then
if v.PrimaryPart:FindFirstChild("PetAlignPosition") and v.PrimaryPart:FindFirstChild("PetAlignOrientation") and v.PrimaryPart:FindFirstChild("PetAttachment") and v:FindFirstChild("PetID") then
if v.PrimaryPart["PetAlignPosition"].Attachment0:IsA("Attachment") and v.PrimaryPart["PetAlignPosition"].Attachment1:IsA("Attachment") and v.PrimaryPart["PetAlignOrientation"].Attachment0:IsA("Attachment") and v.PrimaryPart["PetAlignOrientation"].Attachment1:IsA("Attachment") then
local angle = i * ((2 * math.pi) / #petsTable)
local ap = v.PrimaryPart["PetAlignPosition"].Attachment0
local ac = v.PrimaryPart["PetAlignPosition"].Attachment1
local rayParam = RaycastParams.new()
rayParam.FilterType = Enum.RaycastFilterType.Blacklist
local list = {}
for _, v in pairs(game:GetService("Players"):GetPlayers()) do
for _, q in pairs(v.Character:GetDescendants()) do
if q:IsA("BasePart") then
table.insert(list, q)
end
end
end
for i,v in pairs(game.Workspace:WaitForChild("Pets"):GetDescendants()) do
if v:IsA("BasePart") then
table.insert(list,v)
end
end
rayParam.FilterDescendantsInstances = list
local raycastResult = workspace:Raycast(v.PrimaryPart.Position, v.PrimaryPart.Position - Vector3.new(0, 100, 0), rayParam)
if raycastResult then
local Object = raycastResult.Instance
if Object then
ac.Position = Vector3.new(math.sin(angle) * radius, 0, math.cos(angle) * radius)
ac.WorldPosition = Vector3.new(ac.WorldPosition.X, ((Object.Position.Y + (Object.Size.Y /2)) + (v.PrimaryPart.Size.Y/2)) , ac.WorldPosition.Z)
else
ac.Position = Vector3.new(math.sin(angle) * radius, 0, math.cos(angle) * radius)
end
end
else
game:GetService("Debris"):AddItem(v,0)
end
elseif v:FindFirstChild("PetID") then
v:SetPrimaryPartCFrame(char.PrimaryPart.CFrame)
local angle = i * ((2 * math.pi) / #petsTable)
local ac = Instance.new("Attachment")
ac.Visible = false
ac.Name = v["PetID"].Value
ac.Parent = char.PrimaryPart
local ap = Instance.new("Attachment")
ap.Visible = false
ap.Name = "PetAttachment"
ap.Parent = v.PrimaryPart
local rayParam = RaycastParams.new()
rayParam.FilterType = Enum.RaycastFilterType.Blacklist
local list = {}
for _, v in pairs(game:GetService("Players"):GetPlayers()) do
for _, q in pairs(v.Character:GetDescendants()) do
if q:IsA("BasePart") then
table.insert(list, q)
end
end
end
for i,v in pairs(game.Workspace:WaitForChild("Pets"):GetDescendants()) do
if v:IsA("BasePart") then
table.insert(list,v)
end
end
rayParam.FilterDescendantsInstances = list
local raycastResult = workspace:Raycast(v.PrimaryPart.Position, v.PrimaryPart.Position - Vector3.new(0, 100, 0), rayParam)
if raycastResult then
local Object = raycastResult.Instance
if Object then
ac.Position = Vector3.new(math.sin(angle) * radius, 0, math.cos(angle) * radius)
ac.WorldPosition = Vector3.new(ac.WorldPosition.X, ((Object.Position.Y + (Object.Size.Y /2)) + (v.PrimaryPart.Size.Y/2)) , ac.WorldPosition.Z)
else
ac.Position = Vector3.new(math.sin(angle) * radius, 0, math.cos(angle) * radius)
end
end
local alignPosition = Instance.new("AlignPosition")
alignPosition.Name = "PetAlignPosition"
alignPosition.MaxForce = AlignPosition_MaxForce
alignPosition.Attachment0 = ap
alignPosition.Attachment1 = ac
alignPosition.Responsiveness = AlignPosition_Responsiveness
alignPosition.Parent = v.PrimaryPart
local alignOrientation = Instance.new("AlignOrientation")
alignOrientation.Name = "PetAlignOrientation"
alignOrientation.MaxTorque = AlignOrientation_MaxTorque
alignOrientation.Attachment0 = ap
alignOrientation.Attachment1 = ac
alignOrientation.Responsiveness = AlignOrientation_Responsiveness
alignOrientation.Parent = v.PrimaryPart
else
game:GetService("Debris"):AddItem(v,0)
end
end
end
end
end
end)
PetsFolder.ChildAdded:Connect(function(addedItem)
task.wait()
if addedItem:IsA("Folder") and game.Players:FindFirstChild(tostring(addedItem.Name)) then
addedItem.ChildAdded:Connect(function(addedItem2)
task.wait()
if game.Players:FindFirstChild(addedItem.Name) and addedItem2:IsA("Model") and addedItem2.PrimaryPart then
local function setNetworkOwnerOfModel(model, networkOwner)
for _, descendant in pairs(model:GetDescendants()) do
if descendant:IsA("BasePart") then
if descendant:CanSetNetworkOwnership() then
descendant:SetNetworkOwner(networkOwner)
end
end
end
end
setNetworkOwnerOfModel(addedItem2, game.Players:FindFirstChild(addedItem.Name))
else
game:GetService("Debris"):AddItem(addedItem2,0)
end
end)
end
end)
game.Players.PlayerAdded:Connect(function(plr)
local newPlayerFolder = Instance.new("Folder",PetsFolder)
newPlayerFolder.Name = plr.Name
end)
game.Players.PlayerRemoving:Connect(function(plr)
if PetsFolder:FindFirstChild(plr.Name) then
game:GetService("Debris"):AddItem(PetsFolder[plr.Name], 0)
end
end)