Kill all players when one player touches a part

Hi.

i want that all player get killed if one player touches a part. But i only want that they will get killed one time. So if i touch the part all players get killed one time

local function killEveryone() local allPlayers = game.Players:GetChildren()
for i = 1, #allPlayers do
allPlayers[i].Character.Humanoid.Health = 0
end end

script.Parent.Touched:Connect(killEveryone)

this is my script, but its killing all players everytime. So i cant respawn.

Thanks!

What do you mean by you can’t respawn?

Can we see a video of the issue? that might help, and as Elite said, what do you mean by you can’t respawn?

I mean with i cant respawn that i get killed
continuous, so i cant respawn because then im dead again :smiley:

Just disconnect the Event if you don’t want it to be in use anymore, so that won’t happen.

1 Like

how? :smiley:

can you send the script?

Try this:

local function killEveryone(TouchedCharacter) 
  local player = game.Players:GetPlayerFromCharacter(TouchedCharacter.Parent) or game.Players:GetPlayerFromCharacter(TouchedCharacter.Parent.Parent)
  if player == nil then return end
  local allPlayers = game.Players:GetChildren()
  for i = 1, #allPlayers do
    allPlayers[i].Character.Humanoid.Health = 0
  end 
end

script.Parent.Touched:Connect(killEveryone)

The problem is killing all player even if touched part is not human character

1 Like

You aren’t allowed to asked for Scripts in scripting support, but I can show you an example with explaining it, from which you will basically get all the idea.

local part = script.Parent
local Players = game:GetService("Players") --Define our variables
local Conn

local function killAllPlayers(hit)
    local foundPlayer = Players:GetPlayerFromCharacter(hit.Parent) --Check if the hit part's parent is a player character

    if foundPlayer then --If character then...
         Conn:Disconnect() --Disconnect the connection
         local players = Players:GetPlayers() --Get All players in game

         for _, player in pairs(players) do --Iterate through all players
               player.Character.Humanoid.Health = 0 -- Set health to 0, basically kill them
         end
    end
end

Conn = part.Touched:Connect(killAllPlayers) --Assign the connection to the `Conn` variable