Undescribed Error "attempt to call nil value"

I keep getting this weird error inside my code, "Attempt to call a nil value"

it seems so random, to find it, i had to use quite a bit of print statements, but i managed to find the origin. but not the fix.

Code:

    local Winners = {}
	for i,v in pairs(PlayersTable) do
		Winners[#Winners + 1] = v.Name
	end
	print("Pass2") --// PRINTS THIS
	for i,v in pairs(Players:GetPlayers()) do
		local _,Err = pcall(function()
			v:LoadCharacter()
		end)
		if Err then
			local Humanoid = v.Character:FindFirstChildOfClass("Humanoid")
			if Humanoid then
				Humanoid.Health = 0
			else
				v.Character:BreakJoints()
			end
		end
	end

	print("Pass3") --// IT WONT PRINT THIS

Place a print statement line by line and find where it stops printing, that should be where the error is occurring.

ok, i am getting somewhere.

for i,v in pairs(Players:GetPlayers()) do
		print("Pass1a")
		local _,Err = pcall(function()
			v:LoadCharacter() --// BREAKS
			print("Pass1b") 
		end)
		print("Pass1c")
		if Err then
			print("Pass1d")
			local Humanoid = v.Character:FindFirstChildOfClass("Humanoid")
			print("Pass1e")
			if Humanoid then
				print("Pass1f")
				Humanoid.Health = 0
			else
				print("Pass1g")
				v.Character:BreakJoints()
			end
		end
	end

idk why Player:LoadCharacter() is being faulty.

im pretty speechless how the pcall wrap is being faulty in this situation

maybe try :LoadCharacterBlocking think its similar also is ur script a local one or a server

Its a ServerScript, LoadCharacterBlocking only works on the command bar

I could use Player.Character:BreakJoints() but only as a last resort, as you need to wait for the player to respawn.

i cant think of any reason, maybe try adding wait(1) before it could be that character hasnt been loaded yet

it seems to load the character fine, when it loads me up, that’s when the error occurs, its AFTER the loading. Plus LoadCharacter is already a Yielding function, so it basically waits for you.

Your error means that somewhere in your code something that evaluates to nil has a pair of parentheses after it. Basically, your code is trying to run this:

nil()

There are a few possible places for this error could happen in the code you provided:

for i,v in pairs(Players:GetPlayers()) do
v:LoadCharacter()
local Humanoid = v.Character:FindFirstChildOfClass("Humanoid")
v.Character:BreakJoints()

To find the error, print out the part before the pair of parentheses and see if it equals nil. Example:

--   ...
print(Players.GetPlayers) --or if (Players.GetPlayers == nil) then print("Found nil function!") end
for i,v in pairs(Players:GetPlayers()) do
--   ...

Clicking the error on the script output in studio opens up the script with the error and the output also mentions the line which the error is originating from.

The error is unclickable, all it says is “attempt to call nil value”, no location, no interactables, nothing.

The error you are highlighting basically means you’re trying to call a function on something that isn’t a thing. Are there any other errors originating from the same script?

uhh


seems to print out the print(Player.LoadCharacter) like all the other times it did. its just killed itself again

image
also printed the print(Player.GetPlayers) (top one) and its the same as all the other ones

litterally only this error, somehow originating from :LoadCharacter()

maybe try moving script around to other places like replicated first replicated sotrage whatever

Can you show the output for this:

for i,v in pairs(Players:GetPlayers()) do
    print("Test1")
    print(v, v.Name, v.ClassName);
    v:LoadCharacter()
    print("Test2")
end

And have you tried running it outside of pcall because if it’s none of the other functions then may’be it’s pcall.
It could’ve been accidentally overwritten in the script’s environment