Kill for cash script help

So I’m making a shooting game where players get currency for every time they kill a player, I’m pretty new to scripting so I don’t know how to do this. Any help would be appreciated.

How do you handle damage on your gun?

Sure, here’s an example of how you could give players currency for killing other players in a Roblox shooting game:

-- Configuration
local KILL_REWARD = 50 -- The amount of currency given to the killer per kill
local CURRENCY_NAME = "Coins" -- The name of the currency players receive

-- Function to give a player currency
local function giveCurrency(player, amount)
    local leaderstats = player:FindFirstChild("leaderstats")
    if not leaderstats then
        leaderstats = Instance.new("Folder")
        leaderstats.Name = "leaderstats"
        leaderstats.Parent = player
    end
    
    local currency = leaderstats:FindFirstChild(CURRENCY_NAME)
    if not currency then
        currency = Instance.new("IntValue")
        currency.Name = CURRENCY_NAME
        currency.Value = 0
        currency.Parent = leaderstats
    end
    
    currency.Value = currency.Value + amount
end

-- Function to handle player deaths
local function onPlayerDied(player, killer)
    if killer and killer:IsA("Player") and killer ~= player then
        giveCurrency(killer, KILL_REWARD)
    end
end

-- Connect the onPlayerDied function to the PlayerAdded event
game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        character.Humanoid.Died:Connect(function()
            onPlayerDied(player, player.Character.Humanoid:GetLastDamage())
        end)
    end)
end)

This code listens for when a player dies and gives the player who killed them a certain amount of currency (in this case, 50 coins). The giveCurrency function creates a leaderstats folder for the player if it doesn’t exist, and adds an IntValue to track their currency. The onPlayerDied function checks if the killer is a valid player and adds the currency reward to their leaderstats folder if they are. Finally, the code connects the onPlayerDied function to the Died event of a player’s Humanoid.

Handling damage in a gun in a Roblox game involves several steps:

Setting up the gun’s damage properties: This involves defining the damage value that the gun should inflict on a player or NPC when it hits them. This can be done using a variable in the gun’s script, such as damageAmount, and can be set to a default value or customized for each gun.

Detecting when the gun hits a player or NPC: This is typically done using a Raycast or similar collision detection method, which sends out a “ray” from the gun’s barrel and detects if it collides with any objects in its path.

Applying damage to the player or NPC: Once a hit is detected, the gun’s script should apply the appropriate amount of damage to the target. This can be done by finding the player’s Humanoid or NPC’s Health property and subtracting the damage amount from it.

Here is an example of how this can be implemented in Lua script:

local damageAmount = 10 -- set default damage amount

-- function to handle when a bullet hits an object
local function onBulletHit(hitObject, hitPoint)
    local humanoid = hitObject.Parent:FindFirstChild("Humanoid") -- check if the hit object has a Humanoid

    if humanoid then
        humanoid:TakeDamage(damageAmount) -- apply damage to the humanoid
    end
end

-- set up bullet collision detection
local bullet = script.Parent -- assuming the script is attached to the bullet
bullet.Touched:Connect(onBulletHit)

In this example, the onBulletHit function is called whenever the bullet collides with an object. If the object has a Humanoid, the function applies damage to it using the TakeDamage method. The bullet variable represents the bullet object and is used to connect the Touched event to the onBulletHit function.

is there certain parts i can remove if i have a autosave leaderstats already?

Yeah, theres a few parts you can remove if you do have a leaderstats already!

since when was that a function? i dont see it on the humanoid documentation

It was a function since I’am high, let me fix the code rq…

-- Configuration
local KILL_REWARD = 50 -- The amount of currency given to the killer per kill
local CURRENCY_NAME = "Coins" -- The name of the currency players receive

-- Function to give a player currency
local function giveCurrency(player, amount)
    local leaderstats = player:FindFirstChild("leaderstats")
    if not leaderstats then
        leaderstats = Instance.new("Folder")
        leaderstats.Name = "leaderstats"
        leaderstats.Parent = player
    end

    local currency = leaderstats:FindFirstChild(CURRENCY_NAME) or leaderstats:WaitForChild(CURRENCY_NAME)
    currency.Value = currency.Value + amount
end

-- Function to handle player deaths
local function onPlayerDied(player, killer)
    if killer and killer:IsA("Player") and killer ~= player then
        giveCurrency(killer, KILL_REWARD)
    end
end

-- Connect the onPlayerDied function to the PlayerAdded event
game.Players.PlayerAdded:Connect(function(player)
    local character = player.Character or player.CharacterAdded:Wait()
    local humanoid = character:WaitForChild("Humanoid")

    humanoid.Died:Connect(function()
        onPlayerDied(player, humanoid:GetLastDamage())
    end)
end)

once last question would you put it in a script or local script then in server script service?

1 Like