Hey, helpers!
I have this script that basically shows an image that says "Game Over" when a player dies. However, since my current round script uses resetting to move the players around the map, I only want this script to work when a player is killed by the NPC. I tried using function and it gave me a bunch of errors. Here is the script (that works) without the NPC detection:
game.Players.PlayerAdded:connect(function(player) player.CharacterAdded:connect(function(char) char.Humanoid.Died:connect(function() gameover = Instance.new("ScreenGui") gameover.Parent = player.PlayerGui gameover.Name = "GameOverGui" image = Instance.new("ImageLabel") image.Parent = gameover image.Name = "GameOverImage" image.BackgroundTransparency = 1 image.Image = "http://www.roblox.com/asset/?id=777253063" image.Size = UDim2.new(1,0,1,0) end) end) end)
Here is the one that doesn't work (with detection)
function touched(hit) humanoid = hit.Parent:FindFirstChild("Humanoid") if humanoid ~= nil then game.Players.PlayerAdded:connect(function(player) player.CharacterAdded:connect(function(char) char.Humanoid.Died:connect(function() gameover = Instance.new("ScreenGui") gameover.Parent = player.PlayerGui gameover.Name = "GameOverGui" image = Instance.new("ImageLabel") image.Parent = gameover image.Name = "GameOverImage" image.BackgroundTransparency = 1 image.Image = "http://www.roblox.com/asset/?id=777253063" image.Size = UDim2.new(1,0,1,0) end) end) end)
Have the NPC insert a certain value into the character's humanoid when it damages them and then remove it after a wait()
this way, the value will be there when the .Died
event is triggered and won't be there if another player kills you a second later. You'll have to edit your NPC to work with this but here's an example code of the detection:
game.Players.PlayerAdded:connect(function(player) player.CharacterAdded:connect(function(char) char.Humanoid.Died:connect(function() if char.Humanoid:FindFirstChild("NPCKilled") then gameover = Instance.new("ScreenGui") gameover.Parent = player.PlayerGui gameover.Name = "GameOverGui" image = Instance.new("ImageLabel") image.Parent = gameover image.Name = "GameOverImage" image.BackgroundTransparency = 1 image.Image = "http://www.roblox.com/asset/?id=777253063" image.Size = UDim2.new(1,0,1,0) end end) end) end)