Following a tutorial, got a bug

I am using PolarisProg’s advanced pet system tutorial, but I got this error on this script.

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PetsFolder = workspace:WaitForChild("PlayerPets")

local Spacing = 5
local PetSize = 3
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
		table.insert(Rows, v)
	end
end

local function GetRowWidth(Row, Pet)
	if Pet 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(Deltatime)
	for _, PlayerPetFolder in PetsFolder:GetChildren() do
		if not Players:FindFirstChild(PlayerPetFolder.Name) then return end
		local Character = Players[PlayerPetFolder.Name].Character or Players[PlayerPetFolder.Name].CharacterAdded:Wait()
		local HumanoidRootPart = Character.PrimaryPart
		local Humanoid = Character:WaitForChild("Humanoid")
		if not Character or not Humanoid or not HumanoidRootPart then return end
		local Pets = {}
		local Rows = {}
		for _, v in PlayerPetFolder:GetChildren() do
			table.insert(Pets, v)
		end
		RayParams.FilterDescendantsInstances = {PetsFolder, Character}
		local MaxRowCapacity = math.ceil(math.sqrt(#Pets))
		RearrangeTables(Pets, Rows, MaxRowCapacity)
		for i, Pet in Pets do
			local RowIndex = math.ceil(i / MaxRowCapacity)
			local Row = Rows[RowIndex]
			local RowWidth = GetRowWidth(Row, Pets)
			
			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 = 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)
			
			Pet.PrimaryPart.CFrame = Pet.PrimaryPart.CFrame:Lerp(0.1)
		end
	end
end)

Error (line 29):

Players.Waddletastic.PlayerScripts.PetFollower:29: attempt to index nil with 'Size'  -  Client - PetFollower:29

Most likely because your pet doesn’t have a primary part. Go into your pet model’s properties and set its primaryPart to the main part in the model.

It does. Followed the tutorial 1:1, it told me to make a part, anchor it, disable collisions, group it, set the primarypart and that’s it.

When you call the function GetRowWidth you are sending Pets instead of Pet singular.

local RowWidth = GetRowWidth(Row, Pets)
should be
local RowWidth = GetRowWidth(Row, Pet)

1 Like

Alright, sorry if this is annoying but I got one more issue, I see that same error on the YouTuber’s side but he hasn’t mentioned it and it just works for him

Code:

local X = (table.find(Row, Pet) - 1) * Spacing

Error:

Players.Waddletastic.PlayerScripts.PetFollower:67: attempt to perform arithmetic (sub) on nil and number

Almost got everything fixed.

I’m pretty sure If there are no pets in the PlayerPetFolder then you will get this error.

To prevent this error I’m pretty sure you can check the length of the pet folder and if it is 0 don’t run the code, for example:

if #Pets == 0 then return end

before the for i, Pet in Pets do loop

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.