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

How to activate this script when a player is killed by the NPC?

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)

3 comments

0

Edit your post and paste the error that gives you.

0

I have some few scripts that do something when they die. But the hit function isn't in the humanoid / local humanoid

0

& should there not be two or some more "end"?

1 answer

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)

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