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
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
@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.
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.