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

How would I make a script, every kill gives you gold in leaderboard?

Asked by 4 years ago

I'm new to scripting so I don't know how to do this ;-;

1 answer

Log in to vote
0
Answered by
Geobloxia 251 Moderation Voter
4 years ago
Edited 4 years ago

leaderboard script in ServerScriptService

game.Players.PlayerAdded:Connect(function(player)
    -- function activates when player joins
    local leaderstats = Instance.new("IntValue")
    leaderstats.Name = "leaderstats"
    leaderstats.Value = 0
    leaderstats.Parent = player
    -- creates leaderstats into player

    local gold = Instance.new("IntValue")
    gold.Name = "Gold"
    gold.Value = 0
    gold.Parent = leaderstats
    -- creates gold value in player
end)

Kill script in weapon:

local goldgiven = 2 -- change to change amount
-- of gold given per kill
local tool = script.Parent
-- the tool
local handle = tool:WaitForChild("Handle")
-- the handle or the part that the character sees
local holding = false
-- acts like a debounce

tool.Equipped:Connect(function()
    -- function activates when player
    -- takes out tool
    holding = true
    -- sets holding to true
end)

tool.Unequipped:Connect(function()
    -- function activates when player
    -- unequips the tool
    holding = false
    -- sets holding to false
end)


handle.Touched:Connect(function(hit)
    -- activates when something touched the handle
    if hit.Parent:FindFirstChild("Humanoid") then
        -- if the thing that touched it has a humanoid
        -- it does this
        local hum = hit.Parent:WaitForChild("Humanoid")
        -- finds humanoid and waits for it to load
        if hum.Health == 0 then
            -- if the health of the entity it hit is 0
            -- this happens
            if holding == true then
                -- the player has to be holding the tool
                -- or else the objective won't work and
                -- you have to switch up the variables
                local char = tool.Parent
                -- the character in the workspace
                local plr = game.Players:GetPlayerFromCharacter(char)
                -- takes the player in Players from the character
                local leaderstats = plr:FindFirstChild("leaderstats")
                -- finds leaderstats
                local gold = plr:FindFirstChild("Gold")
                -- finds gold
                if gold and leaderstats then
                    -- if the player has the gold and leaderstats
                    -- then this happens
                    plr.leaderstats.gold.Value = plr.leaderstats.gold.Value + goldgiven
                    -- gives player gold depending on the variable's (goldgiven)
                    -- value. In this case it is 2 gold
            end
            end
        end
    end
end)

Tell me in the comments if it doesn't work.

0
Just edited it. Geobloxia 251 — 4y
0
Thanks, could you add comments explaining what each thing is so I can understand the code better? xDreamingLuma -3 — 4y
0
Sure thing. Geobloxia 251 — 4y
0
Done. Geobloxia 251 — 4y
0
Need some help, could you test this out on a baseplate? It's not working for me, but that could be because im using a Sword tool. xDreamingLuma -3 — 4y
Ad

Answer this question