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

Event for doing something when a player kills someone?

Asked by 10 years ago

I'm trying to give a player xP when they earn a kill.

1
Basically, you need a script in the player who died that uses an ObjectValue to link back to the player who killed him, and from there do whatever is needed to the killer. deaththerapy 60 — 10y

1 answer

Log in to vote
0
Answered by
Azarth 3141 Moderation Voter Community Moderator
10 years ago

When a player kills another player with a weapon that correctly inserts the tag value, you can track who killed who. What is a tag value? That's just what I call it, because to me, it explains exactly what it does, it tags the player so the script can do something with it. This value is called an ObjectValue, an ObjectValue's Value leads back to an Instance, the killer in this case.

When a player kills another player with a ROBLOX linked sword, the victim will receive a 'tag' in their humanoid named 'creator', you can then configure a script to detect when a person dies and check for that tag. For instance;

local function player_died(player_who_died)
    local humanoid = player_who_died.Character["Humanoid"]
    local tag = humanoid:findFirstChild("creator")
    if tag then 
        print("You were murdered!")
        if tag.Value.Name == player_who_died.Name then 
            print("You killed yourself!")
        else
            print(tag.Value.Name.. " murdered you!")
        end
    else
        print("You reset or the weapon you're using doesn't create the tag.")
    end
end

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(c)
        -- Note if you have turned off auto load, you will need a check before this event as well. 
        local hum = c:WaitForChild("Humanoid")
        hum.Died:connect(function() 
            player_died(player) 
        end)
    end)
end)
0
That makes so much more sense, thanks man. Adam335 5 — 10y
Ad

Answer this question