Attempt to index nil with 'Pets'

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    A working pet system

  2. What is the issue?

The issue is it comes up with an error showing “attempt to index nil with ‘Pets’”.

  1. What solutions have you tried so far?
    Finding a solution
local DB = false
local XPgain = 25
local Delay = 0.1

script.GiveXp.OnServerEvent:Connect(function()
			local Player = game.Players.LocalPlayer
			for i,v in pairs(Player.Pets:GetChildren()) do
				if v.Equipped.Value == true then
					v.TotalXP.Value = v.TotalXP.Value + XPgain
				end
			end
			for i,v in pairs(workspace.PlayerPets:FindFirstChild(Player.Name):GetChildren()) do
				spawn(function()
					local Clone = game.ReplicatedStorage.Pets.AddXPdisplay:Clone()
					Clone.Parent = v.PrimaryPart
					for i = 1,25 do
						Clone.StudsOffset = Clone.StudsOffset + Vector3.new(0, .04, 0)
						Clone.TextLabel.TextTransparency = Clone.TextLabel.TextTransparency + .04
						wait(.02)
					end
					Clone:Destroy()
				end)
			end
			DB = true
			wait(Delay)
			DB = false
		end)

Thank you in advance.

Which line is the error showing as on, can you show me your output and the area where the link on the output leads you? I cannot tell which “Pets” part it is, thank you.

image

You cannot access game.Players.LocalPlayer from a script. You can only access it if you are using a local script.

To fix your issue, change line 6 to this one

script.GiveXp.OnServerEvent:Connect(function(Player)

and remove line

local Player = game.Players.LocalPlayer
2 Likes

So how would I do it in a script? game:GetService(“Players”) or something?

I just edited my previous reply, have a look! :smiley:

2 Likes

I will also explain what Valentin did to help you learn;

When you send an event from the client to the server, the client automatically sends which player it is too. So if you call for a variable as the first in the function, you will receive the player instance. (Order matters!)

Examples
Player is the player instance

script.GiveXp.OnServerEvent:Connect(function(Player)

var1 is the player instance

script.GiveXp.OnServerEvent:Connect(function(var1,Player)

Player is the player instance

script.GiveXp.OnServerEvent:Connect(function(Player,var1)

Thank you to @Arlenox 's great response!

2 Likes

Thank you it worked but there is another error

What’s the other error? (Extra Characters)

When ever I click it gives xp but it starts of slow and the goes really fast even when I click slow it is like add more xp to the pet!
https://gyazo.com/ead38686c45a73149a6195a48f0e012a
You might not be able to tell from the little clip.

I don’t really know how your pet system work, therefor I can’t help you;

Here is another clip the pet level starts at 0 and when I click once it goes up by a lot.
https://gyazo.com/2d5bc29bf62f4f6c6d246548a264322d

I assume your pet system stores the amount of XP a pet requires in order to level up.

Example: If a pet needs 5 XP to level, it will automatically get a level because from what I see, your system gives the pet 25 XP immediately.

Do you think this has something to do with it?

function getLevel(totalXP)
	local Increment = 0
	local RequiredXP = 100
	for i = 0, RS.Pets.Settings.MaxPetLevel.Value do
		RequiredXP = 100 + (25*i)
		if totalXP >= (100*i) + Increment then
			if i ~= RS.Pets.Settings.MaxPetLevel.Value then
				if totalXP < ((100*i) + Increment) + RequiredXP then
					return i
				end
			else
				return i
			end
		end
		Increment = Increment+(i*25)
	end
end

function getMaxXP(MaxLevel)
	local MaxXP = 0
	for i = 0,MaxLevel do
		if i == MaxLevel then
			return MaxXP  + (100 * (i+1))
		else
			MaxXP = MaxXP + (i*25)
		end
	end
end

Player:WaitForChild("Pets")

If this results in an infinite yield then the player instance does not have a valid child named “Pets”.

GiveXp is a remote event correct? In the parameters, just do ‘player’ and use it as the player instead of using ‘game.Players.LocalPlayer’ because as it suggests, its local, it does not work in server scripts..

Edit: never mind lol