How do i get player from a table

So im making a function that being called when round ends and i got the error ServerScriptService.Main:372: attempt to index nil with ‘Character’ basically i cant get from the table a player its printing out that its nil i tried with ipairs but it doesnt seem to work
Here is code:

local function OnRoundEnd(player)
	local container = game.Workspace.Map.Container
	local zone2 = Zone.new(container)
	local playersremaining = zone2:getPlayers()
	print(playersremaining)
	if #playersremaining == 1 then
		EndOfTheRoundStatus.Value = "Draw"
		for _, plr in pairs(playersremaining) do
			print(plr)
			local playerBackpack = game.Players:FindFirstChild(plr)
			local playerchar = game.Workspace:FindFirstChild(plr)
			print(playerBackpack)
			print(playerchar)
			playerchar.Character:MoveTo(game.Workspace.Folder.SpawnLocation.Position)
			playerBackpack.Backpack.ClassicSword:Destroy()
			if playerchar.Character:FindFirstChild("ClassicSword") and playerchar.Character.ClassicSword:IsA("Tool") then
				playerchar.Character.ClassicSword:Destroy()
			end
		end
	end
end

Is the plr variable a string or an instance?!

playerchar is already pointing to the players character so your saying player.Character.Character.MoveTo here. Just change it to playerchar.MoveTo

The plr variable is an instance

That is the point. You can’t use FindFirstChild method with an instance as the first parameter. It should be a string like the player’s name in your case.

If you want to get the player’s character, you should instead index the plr variable directly like plr.Character. Remember to check if it exists by implementing an if statement before doing anything with the character model.


Also, I just noticed that you are trying to index the character and the backpack once more with thierselfs, what is that for?


This would be plr.Backpack or plr:FindFirstChildWhichIsA("Backpack").

2 Likes

Thank you man gonna try this code!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.