Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago
Edited 7 years ago

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)

0
Edit your post and paste the error that gives you. WHITEHH 0 — 7y
0
I have some few scripts that do something when they die. But the hit function isn't in the humanoid / local humanoid MineJulRBX 52 — 7y
0
& should there not be two or some more "end"? MineJulRBX 52 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

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)
Ad

Answer this question