Error "attempt to call a nil value"

I have a method that is ran through a module script. It works perfectly when the character is added first time. However if the player dies then the script breaks down trying to run the method saying their is an error attempting to call a nil value.

This is the code on the regular script

function remotes.EquipOnJoin:OnClientInvoke()
	local c = lp.Character
	petC:EquipOnJoin(c) -- this is where it messes up
end

what is the error trying to tell me?

4 Likes

Can you show your function “EquipOnJoin”?

function pets:EquipOnJoin(c)
	if (isServer) then
		local ToChange = 1
		for i,v in pairs(self.petEquipped) do
			self.petEquipped[i] = nil
		end
		for petKey,petData in pairs(self.petNameEquipped) do
			
			local petName = petData.Name
			self.petEquipped["Duck" .. tostring(ToChange)] =  pStorage:FindFirstChild(petName):Clone()
			self.petEquipped["Duck" .. tostring(ToChange)]:FindFirstChild("Head").Name = petKey
			self.petEquipped["Duck" .. tostring(ToChange)].Parent = c
			self.petEquipped["Duck" .. tostring(ToChange)].PrimaryPart.CFrame = c:FindFirstChild("HumanoidRootPart").CFrame + Vector3.new(10,10,10)
			
			
			ToChange = ToChange + 1
		end
	else
		self.petEquipped = remotes.EquipOnJoin:InvokeServer()
	end
	
end

It’s because the character becomes nil. Possibly try making the function make sure it exists first.

repeat wait(1 / 33) until lp.Character -- This would go above the "local c = lp.Character"

didn’t work, it seems to get the character, i can run checks to see if we have it and they all pass.

@LeoBlackbane: Please don’t abuse loops like this to wait for the player. There are events that do this for you, just do local character = lp.Character or lp.CharacterAdded:Wait(), which will wait for a new character if there isn’t one already.


@billyandme: Can you show the full error message? It’s hard to tell if something inside the EquipOnJoin function is erroring, or if petC doesn’t even have an EquipOnJoin function.

5 Likes

Isn’t "petC:EquipOnJoin(c) the error? Because the name of the function itself is pets:EquipOnJoin…

sorry i never posted that i got a solution but all it was is that the local script was a script that got reset on the players death the equiponjoin is the function and when we call petC:EquipOnJoin that is calling the function through the module.