Advertisement
Med_Gaming

Untitled

Apr 29th, 2024 (edited)
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.21 KB | None | 0 0
  1. local RunService = game:GetService("RunService")
  2. local Players = game:GetService("Players")
  3. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  4.  
  5. local PetFolder = game.Workspace:WaitForChild("Units")
  6.  
  7. local Spacing = 4
  8. local MaxClimbHeight = 6
  9.  
  10. local RayParams = RaycastParams.new()
  11. local RayDirection = Vector3.new(0, -500, 0)
  12.  
  13. local function RearrangeTables(Pets, Rows, MaxRowCapacity)
  14.     table.clear(Rows)
  15.     local AmountOfRows = math.ceil(#Pets / MaxRowCapacity)
  16.     for i = 1, AmountOfRows do
  17.         table.insert(Rows, {})
  18.     end
  19.     for i, v in Pets do
  20.         local Row = Rows[math.ceil(i / MaxRowCapacity)]
  21.         table.insert(Row, v)
  22.     end
  23. end
  24.  
  25. local function GetRowWidth(Row, Pet)
  26.     if Pet ~= nil then
  27.         local SpacingBetweenPets = Spacing - Pet.PrimaryPart.Size.X
  28.         local RowWidth = 0
  29.  
  30.         if #Row == 1 then
  31.             return 0
  32.         end
  33.  
  34.         for i,v in Row do
  35.             if i ~= #Row then
  36.                 RowWidth += Pet.PrimaryPart.Size.X + SpacingBetweenPets
  37.             else
  38.                 RowWidth += Pet.PrimaryPart.Size.X
  39.             end
  40.         end
  41.  
  42.         return RowWidth
  43.     end
  44. end
  45.  
  46. RunService.Heartbeat:Connect(function(Delatime)
  47.     for _, PlayerPetFolder in PetFolder:GetChildren() do
  48.         task.spawn(function()
  49.             if not Players:FindFirstChild(PlayerPetFolder.Name) then return end
  50.             local Character = Players[PlayerPetFolder.Name].Character
  51.             if not Character then return end
  52.             local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
  53.             local Humanoid:Humanoid = Character:FindFirstChild("Humanoid")
  54.             if Character == nil or HumanoidRootPart == nil or Humanoid == nil then return end
  55.  
  56.             local Pets = {}
  57.             local Rows = {}
  58.             for _, Pet in PlayerPetFolder:GetChildren() do
  59.                 table.insert(Pets, Pet)
  60.             end
  61.             RayParams.FilterDescendantsInstances = {PetFolder, Character}
  62.             local MaxRowCapacity = math.ceil(math.sqrt(#Pets))
  63.             RearrangeTables(Pets, Rows, MaxRowCapacity)
  64.  
  65.             for i, Pet in Pets do
  66.                 if not Pet:FindFirstChild("HumanoidRootPart") then continue end
  67.                 local RowIndex = math.ceil(i / MaxRowCapacity)
  68.                 local Row = Rows[RowIndex]
  69.                 local RowWidth = GetRowWidth(Row, Pet)
  70.  
  71.                 local XOffset = #Row == 1 and 0 or RowWidth/2 - Pet.PrimaryPart.Size.X/2
  72.                 local X = (table.find(Row, Pet) - 1) * Spacing
  73.                 local Z = RowIndex * Spacing
  74.                 local Y = 0
  75.  
  76.                 local RayResult = game.Workspace:Blockcast(Pet.PrimaryPart.CFrame + Vector3.new(0, MaxClimbHeight, 0), Pet.PrimaryPart.Size, RayDirection, RayParams)
  77.  
  78.                 if RayResult then
  79.                     Y = RayResult.Position.Y + Pet.PrimaryPart.Size.Y/2
  80.                 end
  81.  
  82.                 local TargetCFrame = CFrame.new(HumanoidRootPart.CFrame.X, 0, HumanoidRootPart.CFrame.Z) * HumanoidRootPart.CFrame.Rotation * CFrame.new(X - XOffset, Y, Z)
  83.  
  84.                 if Humanoid.MoveDirection.Magnitude > 0 then
  85.                     local Val = Pet:FindFirstChild("Running") or Instance.new("BoolValue", Pet)
  86.                     Val.Name = "Running"
  87.                     if not Val.Value then
  88.                         Val.Value = true
  89.                         Pet:WaitForChild("Humanoid"):LoadAnimation(script.Anim):Play()                 
  90.                     end
  91.                 else
  92.                     for i, v in pairs(Pet:WaitForChild("Humanoid"):GetPlayingAnimationTracks()) do
  93.                         v:Stop()
  94.                     end
  95.                     game.Debris:AddItem(Pet:FindFirstChild("Running"), 0)
  96.                 end
  97.  
  98.                 Pet.PrimaryPart.CFrame = Pet.PrimaryPart.CFrame:Lerp(TargetCFrame, 0.1)
  99.             end
  100.         end)
  101.     end
  102. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement