How ot make a simple kill all script

Hey everyone i need help with a script of mine the problem is that i want to kill all players but i dont know how to everything i looked up is aways about if player hasnt bough a gamepass they get killed or sum and i just want the simple script that kills all players in server once not multiple time sorry if this is a question that has been awnsered i just cant find any help anywhere i look

for _, player in pairs(game.Players:GetChildren()) do
	local char = player.Character
	if char:WaitForChild("Humanoid").Health > 0 then
		char:WaitForChild("Humanoid").Health = 0
	end
end

Put this script in workspace or ServerScriptService.

3 Likes
for _,v in pairs(game.Players:GetPlayers()) do
       v.Character:BreakJoints()
end

Simplest way i can think of

Player:LoadCharacter()

If you don’t want to index each player’s character.

local players = game:GetService("Players")

for _, player in ipairs(players:GetPlayers()) do
	player:LoadCharacter()
end
1 Like

what do you put in the _ part?
do you put

for i = 1

or something else? im really new to lua

Prerequisite explanation

The example code you gave is a part of a completely separate type of loop called a “numerical for loop”. The loop used in this reply is called a “generic for loop”. It is used for complex iteration. Generic for loops rely on iterator functions, which provide a tuple of information each iteration.

The most common use of a generic for loop is the traversal of a table (see “Iterating over” subsections). The iterator functions Lua(u) provides us for this task are pairs and ipairs, of which both functions provides a 2-value tuple per iteration. The values we receive are as follows:

  1. The key/index of the value in iteration
  2. The value in iteration

Code example:

local letters = {"A", "B", "C"}

for index, value in ipairs(letters) do
    print(index, value)
end

--[[
1    A
2    B
3    C
]]

The why

Often times developers do not need to know or use the key/index of the value in iteration, but we’re forced to include this as a loop variable because we’re unable to access the 2nd value of the tuple, the value in iteration, without accepting the key/index of that value first. This leads to a variable being declared in the scope that is never used.

In any script you write, it’s expected that any declared variable is a variable that will be used somewhere in that script. We violate that expectation in this scenario. Because of this, it became conventional to name un-used but mandatory variables “_”, as it’s the closest legal syntax awardable to a variable name that yields no information (it’s essentially a blank space). Readers of the script can now know to ignore that variable