Attempt to Index local 'player' (a nil value) error

Ok, still learning here but I can’t figure out what is causing the error "Attempt to Index local ‘player’ (a nil value) on the 3rd line.

Basically I am trying to check if/when a child(pet) is being added to the ‘PetsEquiped’ folder and if so then update the walk speed of the character. I have another script that updates this SpeedBoost value for the player already so I just wanted to update the walkspeed value now in this new script.

What am I missing here?

PS: Never used ChildAdded but I think the syntax is right.

local Players = game:GetService("Players")
local player = game.Players.LocalPlayer

				if player~=nil then			
					if player:FindFirstChild("SpeedBoost") then
						print ("found speed boost")
						local spboost = player:WaitForChild("SpeedBoost").Value
						print("sp Boost is "..spboost)
						character.Humanoid.WalkSpeed = character.Humanoid.WalkSpeed + spboost 	
						character.Humanoid.WalkSpeed = character.Humanoid.WalkSpeed							

				print("walkspeed "..character.Humanoid.WalkSpeed)


I can assume that you’re running in this in a server-sided script (normal script). Normal scripts don’t have a LocalPlayer property as the server is not associated with any clients as it runs on a server computer. The solution would be to use the Player added event to get the player as they join, and then you connect the ChildAdded event to the player’s “PetsEquipped”:

          -- code here
1 Like

if this is in a script, you cannot index LocalPlayer on the server. Instead, you can run it in a LocalScript if you want to index the LocalPlayer.

local Players = game:GetService("Players")
local player = game.Players.LocalPlayer

should become:

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:wait()

I don’t know exactly how you add instances to the client, but I’d recommend adding them on server so the instances are replicated to the server and the values can be changed both on the client and the server.

Yeah but the event could happen mid game as player attaches a pet. So does playeradded fire twice?

You can have many connections for PlayerAdded and they all fire once a player joins, but why does it matter?

Also, you’re using ChildAdded incorrectly. ChildAdded returns the child that was added to the part in the function:

An example:

    print(child.Name .. " was parented to " .. part:GetFullName()

The problem is pets aren’t equipped when the player joins the game, therefore this would never pick up that new pets that are added ‘x’ random time later.

So I was trying to use ChildAdded to detect when a pet was attached to the player and then to check if walkspeed needs to be updated.

I will check the syntax for ChildAdded again maybe there is another trigger i can use.