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

End game when a humanoid or humanoids dies?

I made a game when 3 separate teams, the assassins, the protector and the target. I made the game length of 120 seconds or 2 minutes. However the goal of the game is either the target gets killed or all the assassins get eliminated. I also set up a CFrame for the lobby where the players would be teleported to when the game ends (4th-5th line)


team1 = CFrame.new(34.6, 3.09, 23.6)--change to where team1 spawns(red team) Note: use Vector3 cordinates
team2 = CFrame.new(32, 3.09, -26.6)--change to where team2 spawns(blue team)
team3 = CFrame.new(18.2, 0.59, 8.8)-- change to where team3 spawns
lobby = CFrame.new(48.6, 169.7, -13)-- change the the position of the lobby(where the players go before/after the game)
--weapon = game.Lighting["Sword"]-- change "Sword" to the name of the weapon you want players to have during the game. put the weapon in Lighting.if you want no weapons then remove this line.
Maps = {"Map1","Map2","Map3"}-- change Map1,Map2,Map3 to the name of your maps and put them in Lighting. you can add more just add: ,"another map" after Map3 and so on.
random = Maps[math.random(1,#Maps)] 

while true do 
wait(30)-- how long between games.
script.map.Value = random
local m = Instance.new("Message")
m.Parent = game.Workspace
m.Text = ""
wait(5)
m.Text = random..""
wait(4)
g = game.Lighting[script.map.Value]:Clone()
g.Parent = game.Workspace
g:MakeJoints()
m.Text = "Game starting"
wait(3)
m:remove()
--d = weapon:Clone()
--c = weapon:Clone()
local pplz = game.Players:GetChildren()
for i = 1,#pplz do 
if pplz[i].TeamColor == BrickColor.new("Bright red") and pplz[i] ~= nil and pplz[i].Character:FindFirstChild("Torso") ~= nil then-- change to team1's TeamColor
--c.Parent = pplz[i].StarterGear
--d.Parent = pplz[i].Backpack
pplz[i].Character.Torso.CFrame = team1
--d = weapon:Clone()
--c = weapon:Clone()
elseif pplz[i].TeamColor == BrickColor.new("Deep blue") and pplz[i] ~= nil and pplz[i].Character:FindFirstChild("Torso") ~= nil then--change to team2's TeamColor
pplz[i].Character.Torso.CFrame = team2
--d.Parent = pplz[i].Backpack
--c.Parent = pplz[i].StarterGear
elseif pplz[i].TeamColor == BrickColor.new("Pastel violet") and pplz[i] ~= nil and pplz[i].Character:FindFirstChild("Torso") ~= nil then--change to team3's TeamColor
pplz[i].Character.Torso.CFrame = team3
--d.Parent = pplz[i].Backpack
--c.Parent = pplz[i].StarterGear
end end 
wait(120)-- how long each game lasts.
local pplz = game.Players:GetChildren()
for i = 1,#pplz do
if pplz[i] ~= nil and pplz[i].Character:FindFirstChild("Torso") ~= nil then 
--local clean1 = pplz[i].StarterGear:GetChildren()
--clean1[i]:remove()
--local clean2 = pplz[i].Backpack:GetChildren()
--clean2[i]:remove()
pplz[i].Character.Torso.CFrame = lobby
end end

local m = Instance.new("Message")
m.Parent = game.Workspace
m.Text = ""
wait(4)
game.Workspace[script.map.Value]:remove()
--m.Text = winner.." has won!"
m:remove()
end

1 answer

So your goal is to reload everyone's character once someone/everyone dies, I presume. You need to record when all of the targets or all of the assassins have their characters' parents changed, meaning that either they died or they left. To do this you first of all need a list of who these people are, which I can't see provided in the code you pasted.

local targets={}
local assassins={}
-- add the characters of the targets and of the assassins to the corresponding tables
local t=#targets
local a=#assassins
for i,v in ipairs(targets)do
	local con=v.Changed:connect(function(p)
		if p=="Parent"hten
			t=t-1
			if t==0 then
				for _,x in pairs(targets)do
					x:disconnect()
				end
				for _,x in pairs(assassins)do
					x:disconnect()
				end
				endgame()
			end
		end
	end)
	targets[i]=con
end
for i,v in ipairs(assassins)do
	local con=v.Changed:connect(function(p)
		if p=="Parent"hten
			a=a-1
			if a==0 then
				for _,x in pairs(targets)do
					x:disconnect()
				end
				for _,x in pairs(assassins)do
					x:disconnect()
				end
				endgame()
			end
		end
	end)
	assassins[i]=con
end

After you list the characters of targets and assassins and the endgame function, this code counts the number of players in each list, and subtracts one from that number each time one of the characters' parents is changed, until that number hits 0, where the subtracting is stopped and the game ends.

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