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

How to detect when randomly picked "killer" either dies or leaves game?

I am trying to make my loop break whenever the random chosen "killer" either dies or leaves game. I have been trying to figure it out for the last 4 hours and i'm in desperate need for help. Here is the script:

``local go = true

game.Players.PlayerAdded:Connect(function(plr) plr.CharacterAdded:Connect(function(char) while true do while wait(10) do if go == true then go = false local players = game.Players:GetPlayers() local killer = players[math.random(1,#players)] print(killer) game.ReplicatedStorage.KillerName.Value = killer

				--edit killer characteristics--
				local hat = game.ServerStorage.SpartanHelmet

				killer.Character.Humanoid:RemoveAccessories()
				killer.Character:WaitForChild("Shirt").ShirtTemplate = "http://www.roblox.com/asset/?id=11356448"
				killer.Character:WaitForChild("Pants").PantsTemplate = "http://www.roblox.com/asset/?id=11357134"
				local hatclone = hat:Clone()
				killer.Character.Humanoid:AddAccessory(hatclone)

				--add speed,health and jumpheight--
				killer.Character.Humanoid.WalkSpeed = 24
				killer.Character.Humanoid.MaxHealth = 200
				killer.Character.Humanoid.Health = 200
				killer.Character.Humanoid.JumpHeight = 10

				--show players name--
				local swordsmansign = game.ServerStorage.SwordsmanSign:Clone()
				swordsmansign.Parent = killer.Character:FindFirstChild("Head")

				--show killers picture--
				local Players = game:GetService("Players")
				local board = game.Workspace.Board.board.SurfaceGui.ImageLabel
				local userId = killer.UserId

				local thumbType = Enum.ThumbnailType.HeadShot
				local thumbSize = Enum.ThumbnailSize.Size420x420
				local content, isReady = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)

				board.Image = content
				board.Size = UDim2.new(0, 420, 0, 420)


				--show killers name--
				local label = game.Workspace.LabelPart.SurfaceGui.TextLabel
				label.Text = killer.Name

				--mapchange--
				local function selectmap()
					local maps = game.ReplicatedStorage.Maps:GetChildren()
					local selected = maps[math.random(1,#maps)]
					return selected
				end

				local randomizedmap = selectmap()
				print(randomizedmap)

				local randomizedmapclone = randomizedmap:Clone()
				randomizedmapclone.Parent = game.Workspace.Map
				
				print("Map installed")
				
				--I am trying to detect, if killer is still here then if their health is down to 0, then  break. but if killer left game then break.
			end
		end

	end

end)

end)

1 comment

0

Im trying to do it at the end of the script, where i commented

2 answers

Sooo You Can Use PlayerRemoving

Example

game.Players.PlayerRemoving:Connect(function(plr)
	
	print(plr.Name.." Just Left The Game!")

end)

And For Died You Can Just Disable The Reset Button

5 comments

0

for dying, can use humanoid.Died like this: (insert killer here).Character.Humanoid.Died:Connect(function() -- code here end)

0

Oh Yea I Didnt Used It Hehe Thanks

0

Cant put a break inside a function though. an error would occur saying that the break needs to be in the loop.

0

bruh

0

You Cant Put A Break In A Function Its Only For Loops

local go = true

game.Players.PlayerAdded:Connect(function(plr) 
plr.CharacterAdded:Connect(function(char) 
while true do
while wait(10)  do 
if go == true then 
go = false 
local players = game.Players:GetPlayers() 

local killer = players[math.random(1,#players)] 
print(killer.Name) game.ReplicatedStorage.KillerName.Value = killer.Name

 local hat = game.ServerStorage:WaitForChild(SpartanHelmet)
killer.Character.Humanoid:RemoveAccessories()

if killer.Character:FindFirstChildWhichIsA("Shirt") then
killer.Character:WaitForChild("Shirt").ShirtTemplate = "http://www.roblox.com/asset/?id=11356448"
end

if killer.Character:FindFirstChildWhichIsA("Pants") then
killer.Character:WaitForChild("Pants").PantsTemplate = "http://www.roblox.com/asset/?id=11357134"
end

local hatclone = hat:Clone()
killer.Character.Humanoid:AddAccessory(hatclone)

killer.Character.Humanoid.WalkSpeed = 24
killer.Character.Humanoid.MaxHealth = 200
killer.Character.Humanoid.Health = 200
killer.Character.Humanoid.JumpHeight = 10

local swordsmansign = game.ServerStorage.SwordsmanSign:Clone()
swordsmansign.Parent = killer.Character:FindFirstChild("Head")

local Players = game:GetService("Players")
local board = game.Workspace.Board.board.SurfaceGui.ImageLabel
local userId = killer.UserId
 
local thumbType = Enum.ThumbnailType.HeadShot
local thumbSize = Enum.ThumbnailSize.Size420x420
local content, isReady = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
  
board.Image = content
board.Size = UDim2.new(0, 420, 0, 420)

local label = game.Workspace.LabelPart.SurfaceGui.TextLabel
label.Text = killer.Name

local function selectmap()
local maps = game.ReplicatedStorage.Maps:GetChildren()
local selected = maps[math.random(1,#maps)]
return selected
 end

local randomizedmap = selectmap()
print(randomizedmap.Name)

local randomizedmapclone = randomizedmap:Clone()
randomizedmapclone.Parent = game.Workspace.Map
print("Map installed")

-- create another loop, constantly creating criteria to be met, if its not, then break

while true do
wait(1) -- how much we want to wait before checking again
local met = false

if killer then -- check if player is still in the server
local hum = killer.Character:FindFirstChild("Humanoid") -- check for humanoid
if hum then
if hum.Health > 0 then -- if player isnt dead
met = true -- continue checking
end
end
end

if met == false then -- if criteria is not met, then break
break
end
end

-- continue doing your thing after killer has died or left the game

end
end
end
end)

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