Trying to kill all players if a certain player resets

hello! im trying to make a script that when a player with a certain username dies, it will kill all the other players. i tried putting in a print statement to see if the player that died was the username then it would print but it doesnt work.

script:

local event = game.ReplicatedStorage:WaitForChild("killPlrs")
--local db = false

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		
if plr.Name == "targetframed" or "Player1" then
		local hum:Humanoid = char:WaitForChild("Humanoid")
		
		if hum.Health <= 0 then
			print("plr has died.")
			end
		end
	end)
end)
1 Like

If statements don’t run costantly, they only run once alongside the rest of the code (in this case, your statement will run every time the player character is added)

This should work:

local event = game.ReplicatedStorage:WaitForChild("killPlrs")
--local db = false

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		if plr.Name == "targetframed" or "Player1" then
			local hum:Humanoid = char:WaitForChild("Humanoid")
			
			hum.Died:Once(function()
				print("plr has died.")
			end)
		end
	end)
end)

This was, every time the character dies we’ll run the print line.

EDIT: More performing/correct version:

local event = game.ReplicatedStorage:WaitForChild("killPlrs")
--local db = false

game.Players.PlayerAdded:Connect(function(plr)
	if plr.Name == "targetframed" or plr.Name == "Player1" then
		plr.CharacterAdded:Connect(function(char)

			local hum:Humanoid = char:WaitForChild("Humanoid")
			
			hum.Died:Once(function()
				print("plr has died.")
			end)
		end
	end)
end)
2 Likes

If you want it to be every time the character dies, use :Connect(), not :Once().

1 Like

The connection with :Once() is made each time the character is added, hence why it’s placed inside plr.CharacterAdded.
Using :Connect() (without disconnecting istantly) would create multiple connections without deleting any of them, causing memory leaks.

1 Like

@lamonLikeTinyGamer @12345koip

hey guys, tysm 4 the replies. didnt know there was a :Once() function until u said it so ty 4 that!

do u guys know about how would u go about killing all players if a certain player resets?

1 Like

Instead of the print:

for _, player in game.Players:GetPlayers() do
	player.Character.Humanoid.Health = 0
end

This will kill players regardless if they have a forcefield or not. If you instead want to only kill those without forcefields then do:

for _, player in game.Players:GetPlayers() do
	player.Character.Humanoid:TakeDamage(100) -- 100 is the default max health, feel free to change it
end
1 Like

u absolute wizard bro haha. i thought i need a remote event and stuff. i almost had it, just didn’t structure it properly. tysm 4 helping me out lamon, u saved me so much time. :smiley:

full script:

game.Players.PlayerAdded:Connect(function(plr)
	if plr.Name == "targetframed" or plr.Name == "Player1" then -- change ur username to whatever
		plr.CharacterAdded:Connect(function(char)

			local hum:Humanoid = char:WaitForChild("Humanoid")

			hum.Died:Once(function()
				print("plr has died.")
				
				for _, plr in game.Players:GetPlayers() do
					plr.Character.Humanoid.Health = 0
				end
			end)
		end)
	end
end)
1 Like

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