Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local RunService = game:GetService("RunService")
- local Players = game:GetService("Players")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local PetFolder = game.Workspace:WaitForChild("Units")
- local Spacing = 4
- local MaxClimbHeight = 6
- local RayParams = RaycastParams.new()
- local RayDirection = Vector3.new(0, -500, 0)
- local function RearrangeTables(Pets, Rows, MaxRowCapacity)
- table.clear(Rows)
- local AmountOfRows = math.ceil(#Pets / MaxRowCapacity)
- for i = 1, AmountOfRows do
- table.insert(Rows, {})
- end
- for i, v in Pets do
- local Row = Rows[math.ceil(i / MaxRowCapacity)]
- table.insert(Row, v)
- end
- end
- local function GetRowWidth(Row, Pet)
- if Pet ~= nil then
- local SpacingBetweenPets = Spacing - Pet.PrimaryPart.Size.X
- local RowWidth = 0
- if #Row == 1 then
- return 0
- end
- for i,v in Row do
- if i ~= #Row then
- RowWidth += Pet.PrimaryPart.Size.X + SpacingBetweenPets
- else
- RowWidth += Pet.PrimaryPart.Size.X
- end
- end
- return RowWidth
- end
- end
- RunService.Heartbeat:Connect(function(Delatime)
- for _, PlayerPetFolder in PetFolder:GetChildren() do
- task.spawn(function()
- if not Players:FindFirstChild(PlayerPetFolder.Name) then return end
- local Character = Players[PlayerPetFolder.Name].Character
- if not Character then return end
- local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
- local Humanoid:Humanoid = Character:FindFirstChild("Humanoid")
- if Character == nil or HumanoidRootPart == nil or Humanoid == nil then return end
- local Pets = {}
- local Rows = {}
- for _, Pet in PlayerPetFolder:GetChildren() do
- table.insert(Pets, Pet)
- end
- RayParams.FilterDescendantsInstances = {PetFolder, Character}
- local MaxRowCapacity = math.ceil(math.sqrt(#Pets))
- RearrangeTables(Pets, Rows, MaxRowCapacity)
- for i, Pet in Pets do
- if not Pet:FindFirstChild("HumanoidRootPart") then continue end
- local RowIndex = math.ceil(i / MaxRowCapacity)
- local Row = Rows[RowIndex]
- local RowWidth = GetRowWidth(Row, Pet)
- local XOffset = #Row == 1 and 0 or RowWidth/2 - Pet.PrimaryPart.Size.X/2
- local X = (table.find(Row, Pet) - 1) * Spacing
- local Z = RowIndex * Spacing
- local Y = 0
- local RayResult = game.Workspace:Blockcast(Pet.PrimaryPart.CFrame + Vector3.new(0, MaxClimbHeight, 0), Pet.PrimaryPart.Size, RayDirection, RayParams)
- if RayResult then
- Y = RayResult.Position.Y + Pet.PrimaryPart.Size.Y/2
- end
- local TargetCFrame = CFrame.new(HumanoidRootPart.CFrame.X, 0, HumanoidRootPart.CFrame.Z) * HumanoidRootPart.CFrame.Rotation * CFrame.new(X - XOffset, Y, Z)
- if Humanoid.MoveDirection.Magnitude > 0 then
- local Val = Pet:FindFirstChild("Running") or Instance.new("BoolValue", Pet)
- Val.Name = "Running"
- if not Val.Value then
- Val.Value = true
- Pet:WaitForChild("Humanoid"):LoadAnimation(script.Anim):Play()
- end
- else
- for i, v in pairs(Pet:WaitForChild("Humanoid"):GetPlayingAnimationTracks()) do
- v:Stop()
- end
- game.Debris:AddItem(Pet:FindFirstChild("Running"), 0)
- end
- Pet.PrimaryPart.CFrame = Pet.PrimaryPart.CFrame:Lerp(TargetCFrame, 0.1)
- end
- end)
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement