Attempt to index function with 'LoadCharacter'

So I made a round-based game with a module that does all sorts of things. The problem lies with one function where I reload the character when the round is over. Instead, an error pops up:

The module function:

function playersInRound.RemovePlayer()

	for _, connection in pairs(connections) do 
		connection:Disconnect()
	end

	for _, player in pairs(playersInRound)do
		player:LoadCharacter()
	end	
	playerInfo.InRound = false

end 

The problem lies with the code player:LoadCharacter(). I tried searching up how to fix this error but I was unable to find any that was related to my problem. How would I be able to fix this?

Where did you define player in the script?

Try passing the player argument in the function

The players are in a table module (playersInRound). I used a for i, v in pairs loop to get each player one by one to reload back into the spawns once the round is over.

It’s probably the way you set your code up, did you name the table module the same as the variable the holds the players ingame?

No, would you like to see the whole module?

Would help to figure out the error, so sure. But your error stems from trying to get the players from the module itself, since you’re giving playersInRound, the module, as the table in the in pairs

local playersInRound = {}
local connections = {}

local playerInfo = { 
	AFK = false; 
	InRound = false 
}

function playersInRound.ReturnPlayersInfo()
	return playerInfo
end

function playersInRound.AddPlayer(player)
	if player:IsA("Player") then
		if playerInfo.AFK == false then
			playerInfo.InRound = true
			print(player)
			table.insert(playersInRound, player)
			connections[player.Name] = player.Character.Humanoid.Died:Connect(function()
				table.remove(playersInRound, table.find(playersInRound, player))
			end)
		elseif  playerInfo.AFK == true then
			warn("Player is afk. Was not added to the list")
			playerInfo.InRound = false
		end	
	end
end

function playersInRound.PlayerLeft()
	connections["Removing"] = game.Players.PlayerRemoving:Connect(function(player) 
		local playerLeft = table.find(playersInRound, player)
		if playerLeft then
			table.remove(playersInRound, playerLeft)
			playerInfo.InRound = false
		end
	end)
end

function playersInRound.CheckWinner(status)
	if #playersInRound > 1 then
		while #playersInRound >= 2 do
			status.Value = "Game in progress"
			wait()
			if #playersInRound == 1 then
				local player = playersInRound[1]
				print("The Winner is: " .. player.Name)
				status.Value = player.Name .. " has won this round!"
				wait(5)
			elseif #playersInRound == 0 then
				print("There was not a single winner.")
				status.Value = "No one won this round"
				wait(5)
			end
		end
	end
end

function playersInRound.RemovePlayer()
	-- Kill the players
	for _, connection in pairs(connections) do 
		connection:Disconnect()
	end

	for _, player in pairs(playersInRound)do
		player:LoadCharacter()
	end	
	playerInfo.InRound = false

end 

return playersInRound

Pretty sure that had nothing to do with it, as LoadCharacter loads the character, which shouldn’t need a character to exist beforehand

1 Like

Oh your issue is that you don’t have a specific table for the players, so w hen you do playersInRound, it’s getting everything, including the functions. So just make a specific table, such as playersInRound.Players, put the players in that and reference that instead

1 Like

You’re using the same table that your module is returning, when running your pairs loop your module functions may error the code.

Try defining the table that holds the player objects as a separate table.

1 Like

Wait so does that mean I have to make another specific function for the players???

No, you make another table,

playersInRound.Players = {} 

--or just

local Players = {}

And then reference that for when you add a player or remove a player

1 Like

I added the table, did some adjustments and it works! Thanks for your help! I really appreciate it! I was just confused and may have mixed the 2 together.

1 Like