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

Can you tell me how to code a script to get leaderstats for a kill?

Asked by 3 years ago
Edited 3 years ago

Greetings Everyone,

I need some help with this code I have. I want it that if player1 kills player2 then player1 will get some leaderstats, In my case 1 Kill and 25 Coins. So I want to integrate this into my main game script where it already has a function that runs whenever someone kills another person or when a person dies. So this script is not completely coded by me I have just made countless modifications to it but I cant seem to make the modification I talked about above. If anyone can help me it will be Extremely Appreciated! If you have any questions about what I exactly want, just ask! PLEASE VIEW LINES 37 - 43 THAT IS WHERE I NEED HELP! Here is the code:

local dataStores = game:GetService("DataStoreService"):GetDataStore("WinsDataStore")
local dataStores = game:GetService("DataStoreService"):GetDataStore("CoinsDataStore")
local defaultWins = 0
local playersLeft = 0
game.Players.PlayerAdded:Connect(function(player)

    playersLeft = playersLeft + 1

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

    local wins = Instance.new("IntValue")
    wins.Name = "Wins"
    wins.Value = 0
    wins.Parent = leaderstats

    local coins = Instance.new("IntValue")
    coins.Name = "Coins"
    coins.Value = 0
    coins.Parent = leaderstats


    local playerData = Instance.new("Folder")
    playerData.Name = player.Name
    playerData.Parent = game.ServerStorage.PlayerData

    local equipped = Instance.new("StringValue")
    equipped.Name = "Equipped"
    equipped.Parent = playerData

    local inventory = Instance.new("Folder")
    inventory.Name = "Inventory"
    inventory.Parent = playerData


    player.CharacterAdded:Connect(function(character)
        character.Humanoid.Died:Connect(function()

            if character.Humanoid and character.Humanoid:FindFirstChild("creator") then
                game.ReplicatedStorage.Status.Value = tostring(character.Humanoid.creator.Value).. " Killed "..player.Name
            end



            if character:FindFirstChild("GameTag") then
                character.GameTag:Destroy()
            end

            player:LoadCharacter()
        end)

    end)

    -- Data Stores

    local player_data
    local weaponsData
    local equippedData
    local coins_data

    pcall(function()
        player_data = dataStores:GetAsync(player.UserId.."-Wins")
    end)

        pcall(function()
        coins_data = dataStores:GetAsync(player.UserId.."-Coins")
    end)

    pcall(function()
        weaponsData = dataStores:GetAsync(player.UserId.."-Weps")
    end)

    pcall(function()
        equippedData = dataStores:GetAsync(player.UserId.."-EquippedValue")
    end)

    if player_data ~= nil then
        -- Player has saved data , load it in
        wins.Value = player_data
        coins.Value = coins_data
    else
        -- New player
        wins.Value = defaultWins
        coins.Value = defaultWins
    end

    if weaponsData then

        for _, weapon in pairs(weaponsData) do
            if game.ServerStorage.Items:FindFirstChild(weapon) then
                local weaponClone = game.ServerStorage.Items[weapon]:Clone()
                weaponClone.Parent = inventory
                print(weapon.." loaded in!")
            end
        end

        if equippedData then equipped.Value = equippedData
            player:WaitForChild("PlayerGui")
            game.ReplicatedStorage.SendEquipped:FireClient(player,equippedData)
        end

    else
        print("No weapons data")
    end

end)

local bindableEvents = Instance.new("BindableEvent")

game.Players.PlayerRemoving:Connect(function(player)

    pcall(function()
        dataStores:SetAsync(player.UserId.."-Wins",player.leaderstats.Wins.Value)
    end)

    pcall(function()
        local weapons = game.ServerStorage.PlayerData[player.Name].Inventory:GetChildren()
        local weaponsTable = {}

        for _, v in pairs(weapons) do
            table.insert(weaponsTable,v.Name)
        end

        dataStores:SetAsync(player.UserId.."-Weps",weaponsTable)

        if game.ServerStorage.PlayerData[player.Name].Equipped.Value ~= nil then
            local equippedVal = game.ServerStorage.PlayerData[player.Name].Equipped
            dataStores:SetAsync(player.UserId.."-EquippedValue",equippedVal.Value)
        end
    end)    

    playersLeft = playersLeft - 1
    bindableEvents:Fire()
end)

game:BindToClose(function()
    -- This will be triggered upon shutdown
    while playersLeft > 0 do
        bindableEvents.Event:Wait()
    end
end)

Thankyou for any help, epicnmac

0
Please view lines 37 - 43 that is where I need help! epicnmac 63 — 3y
0
If anyone knows the answer please write it down!???? epicnmac 63 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

Here is a kill and death leader stats script:

local Players = game.Players

local Template = Instance.new 'BoolValue'
Template.Name = 'leaderstats'

Instance.new('IntValue', Template).Name = "Kills"
Instance.new('IntValue', Template).Name = "Deaths"


Players.PlayerAdded:connect(function(Player)
    wait(1)
    local Stats = Template:Clone()
    Stats.Parent = Player
    local Deaths = Stats.Deaths
    Player.CharacterAdded:connect(function(Character)
        Deaths.Value = Deaths.Value + 1
        local Humanoid = Character:FindFirstChild "Humanoid"
        if Humanoid then
            Humanoid.Died:connect(function()
                for i, Child in pairs(Humanoid:GetChildren()) do
                    if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
                        local Killer = Child.Value
                        if Killer:FindFirstChild 'leaderstats' and Killer.leaderstats:FindFirstChild "Kills" then
                            local Kills = Killer.leaderstats.Kills
                            Kills.Value = Kills.Value + 1
                        end
                        return -- Only one player can get a KO for killing a player. Not 2, not 3. Only one.
                    end
                end
            end)
        end
    end)
end)
-- Coded by JulienDethurens using gedit.
0
Oh thanks epicnmac 63 — 3y
0
Ok so how can I insert this into my script? epicnmac 63 — 3y
0
It didnt work epicnmac 63 — 3y
Ad

Answer this question