Ad
Log in to vote
0

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

Asked by 1 year ago

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)

0
Im trying to do it at the end of the script, where i commented Carlowskey 20 — 1y

2 answers

Log in to vote
1
Answered by 1 year ago
Edited 1 year ago

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

0
for dying, can use humanoid.Died like this: (insert killer here).Character.Humanoid.Died:Connect(function() -- code here end) NGC4637 602 — 1y
0
Oh Yea I Didnt Used It Hehe Thanks kidsteve923 139 — 1y
0
Cant put a break inside a function though. an error would occur saying that the break needs to be in the loop. Carlowskey 20 — 1y
0
bruh kidsteve923 139 — 1y
0
You Cant Put A Break In A Function Its Only For Loops kidsteve923 139 — 1y
Ad
Log in to vote
0
Answered by 1 year ago
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)

Answer this question