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

How to make it so if a player kills somebody then the player gets 50 cash?

The Leaderstats are called Cash

2 answers

✓ Accepted answer

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:

local Players = game:GetService("Players")

local function leaderboardSetup(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"

    -- Stats are individual values such as an IntValue
    local cash = Instance.new("IntValue")
    cash.Name = "Cash"

    cash.Parent = leaderstats
    leaderstats.Parent = player
end

-- Connect the "leaderboardSetup()" function to the "PlayerAdded" event
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:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local humanoid = character.Humanoid

        humanoid.Died:Connect(function()
            local tag = humanoid:FindFirstChild("creator")
            
            if tag and tag.Value then
                tag.Value.leaderstats.Cash.Value += 50
            end
        end)
    end)
end)

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

local Players = game:GetService("Players")
local Debris = game:GetService("Debris")

local tool = script.Parent
local handle = tool:WaitForChild("Handle")

local Damage_Per_Hit = 10

-- I just stole the TagHumanoid() and UntagHumanoid() functions from the classic sword

-- Inserts a new "ObjectValue" inside of the humanoid with the tagger's player object
local function TagHumanoid(humanoid, player)
    local creatorTag = Instance.new("ObjectValue")
    creatorTag.Name = "creator"
    creatorTag.Value = player
    Debris:AddItem(creatorTag, 2)
    creatorTag.Parent = humanoid
end

local function UntagHumanoid(humanoid) -- Destroys the tag inside of the humanoid
    for i, v in pairs(humanoid:GetChildren()) do
        if v:IsA("ObjectValue") and v.Name == "creator" then
            v:Destroy()
        end
    end
end

local function OnEquipped()
    local character = tool.Parent
    player = Players:GetPlayerFromCharacter(character)
end

local function OnTouched(hit)
    local victimCharacter = hit.Parent
    local victimPlayer = Players:GetPlayerFromCharacter(victimCharacter)
    
    if victimPlayer then
        local humanoid = victimCharacter.Humanoid

        UntagHumanoid(humanoid)
        TagHumanoid(humanoid,player)
        humanoid:TakeDamage(Damage_Per_Hit)
    end
end

tool.Equipped:Connect(OnEquipped)

handle.Touched:Connect(OnTouched)
player.CharacterAdded:Connect(function(Character)
		Character.Humanoid.Died:Connect(function(Died)
			local creator = Character.Humanoid:FindFirstChild("creator")
			local leaderstats = creator.Value:FindFirstChild("leaderstats")
			if creator ~= nil and creator.Value ~= nil then
				leaderstats.Coins.Value = leaderstats.Coins.Value + 10 -- change to whatever amount of coins you want to award also change "coins" to "cash"
			end
		end)

This is what I use as a KillForCash script hope this helps.

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