A simple script error probably idk

OK.
So i have a script here that’s supposed to kill off all the players in the server. But the script doesnt work. Its a server script. I have already tried waitforchild and findfirstchild, doesnt work. I get this error message:
Players.trueblockhead101.PlayerGui.Countdown.TextLabel.Script:72: attempt to index nil with ‘Humanoid’

Which i think it belives that humanoid doesnt exist but i can ensure you that it does.

local plr2 = plr.Character.Humanoid
	plr2.Health = 0

plr is game.Players:GetPlayers()

1 Like

try local plr2 = plr.LocalPlayer.Character.Humanoid

2 Likes

How exactly are you killing each player? Are you looping through players in a player list?

1 Like

But if you are trying to kill everyone, a simpler way would be to do this:

for i,v in pairs(game.Players:GetChildren()) do
	v.Character.Humanoid.Health = 0
end
1 Like

Doesn’t work on server scripts, only on local scripts, and I doubt they’re using a local script.

1 Like

For the Players service you should use :GetPlayers() instead of GetChildren().

1 Like

That variable is a table. If you wanna kill a specific player, do local player = game.Players:FindFirstChild("player name") then set their health to 0.

1 Like

Can you maybe show more of the script so we can tell where you’re getting the plr variable from?

1 Like

Hey! You can simply use something like this:

for i, v in pairs (game:GetService("Players"):GetPlayers()) do
	v.Character:FindFirstChild("Humanoid").Health = 0
end

Hope this helps!

1 Like

Hey! it works! thanks. Sorry I couldnt respond earlier. Could you explain the whole for i, v in pairs works? I see it everywhere yet dont know what id does…

i stands for the number in which the v (value) was found.

1 Like

It simply loops through everything in a table.

1 Like

It loops through everything inside of game:GetService("Players"):GetPlayers(), i is just the variable for how many times it loops, as in how many players there are in the server, and the v is the instance that it looped to, basically just the player in this scenario. For example you could do:

for playerCount, player in ipairs(game:GetService("Players"):GetPlayers()) do
	player.Character:FindFirstChild("Humanoid").Health = 0
end

or if you’re looping through a model and all of it’s children you could do:

for partNumber, part in pairs(game.Workspace.Folder:GetChildren()) do
	print(part.Name)
	print(partNumber)
end

You could read this article for more information: Introduction to Scripting | Roblox Creator Documentation

oh so if i wanted to destroy a bunch of welds in a model I could use this. Ok I get it thanks.

1 Like