Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is my for loop not working and why isn't the character being found?

Thanks for helping if you're reading this! The problem is that the for loop isn't working when I reference the players and it keeps returning this error in the studio.

12:57:36.608 - ServerScriptService.Script:28: attempt to index field 'Character' (a nil value)

Do any of you know what's wrong with this for loop? Or am I calling the player incorrectly?

while true do
	wait(1/30)
	local Players = game.Players:GetPlayers()
		
	for i=1, #Players do
		wait(10)
		if Players.Character:WaitForChild("Humanoid") then
			Players.Character.Humanoid.Died:Connect(function()
				print (Players.." has died!")
				
			end)			
			
		end
		
		
	end
end

1 answer

Players is a list. A list doesn't have a character. Thus you get the error.

You need to get a player that's in the list, not the list itself.

To get something something from a list, you just use its position in that list.

list[1] --first item
list[2] --second item
list[3] --third item
--etc.

But since you're using a for loop, you can just use the i variable. Since i continuously increments, you'll eventually be affecting all players.

Players[i] --that gives you the current player from the 'Players' list.


Side note, it doesn't make much sense to connect an event inside a while loop. Because now you're going to have a ton of Died events connected to each humanoid.

This discussion is preserved as it was. New answers, comments, and votes are closed.