The easiest way to do this is to use leader stats and tagging.
First, you'll need to set up the leaderboard. To do this, you'll need to insert a folder named "leaderstats" with an IntValue called "Cash" into every player.
Server Script:
01 | local Players = game:GetService( "Players" ) |
03 | local function leaderboardSetup(player) |
04 | local leaderstats = Instance.new( "Folder" ) |
05 | leaderstats.Name = "leaderstats" |
08 | local cash = Instance.new( "IntValue" ) |
11 | cash.Parent = leaderstats |
12 | leaderstats.Parent = player |
16 | Players.PlayerAdded:Connect(leaderboardSetup) |
Next, you'll want to listen for when a player dies. One way to do this is to use the "Died" event in the humanoid. Once the "Died" event is fired, give the tagger 50 cash.
Server Script:
01 | local Players = game:GetService( "Players" ) |
03 | Players.PlayerAdded:Connect( function (player) |
04 | player.CharacterAdded:Connect( function (character) |
05 | local humanoid = character.Humanoid |
07 | humanoid.Died:Connect( function () |
08 | local tag = humanoid:FindFirstChild( "creator" ) |
10 | if tag and tag.Value then |
11 | tag.Value.leaderstats.Cash.Value + = 50 |
Now you'll need to put a tag inside of a player when they get hit. ROBLOX endorsed weapons such as the classic sword do this automatically, but you can use these functions inside of your own weapon's hit logic.
At it's most basic level, a sword script should look like this.
Weapon Script (Server Script):
01 | local Players = game:GetService( "Players" ) |
02 | local Debris = game:GetService( "Debris" ) |
04 | local tool = script.Parent |
05 | local handle = tool:WaitForChild( "Handle" ) |
07 | local Damage_Per_Hit = 10 |
12 | local function TagHumanoid(humanoid, player) |
13 | local creatorTag = Instance.new( "ObjectValue" ) |
14 | creatorTag.Name = "creator" |
15 | creatorTag.Value = player |
16 | Debris:AddItem(creatorTag, 2 ) |
17 | creatorTag.Parent = humanoid |
20 | local function UntagHumanoid(humanoid) |
21 | for i, v in pairs (humanoid:GetChildren()) do |
22 | if v:IsA( "ObjectValue" ) and v.Name = = "creator" then |
28 | local function OnEquipped() |
29 | local character = tool.Parent |
30 | player = Players:GetPlayerFromCharacter(character) |
33 | local function OnTouched(hit) |
34 | local victimCharacter = hit.Parent |
35 | local victimPlayer = Players:GetPlayerFromCharacter(victimCharacter) |
38 | local humanoid = victimCharacter.Humanoid |
40 | UntagHumanoid(humanoid) |
41 | TagHumanoid(humanoid,player) |
42 | humanoid:TakeDamage(Damage_Per_Hit) |
46 | tool.Equipped:Connect(OnEquipped) |
48 | handle.Touched:Connect(OnTouched) |