'How to make other scripts change leaderstats?

im playing roblox and i wanna make a script so when you sell your capacity your money goes up by that amount. ill provide all scripts below.

local MaxCapacity = 5 --sets the maximum amount of times you can click. (can change)
local Capacity = 0 --the default capacity (dont change)
script.Parent.Activated:Connect(function()
    if Capacity < 5 then -- change the number to your max capacity
        Capacity = Capacity + 1
        print((Capacity))
    else
        if Capacity == 5 then -- change the number to your max capacity
            print("you have reached max capacity, go sell it.")
        end
    end
end)

-- the sell script:

game.Workspace["sell part"].ClickDetector.MouseClick:Connect(function()
    if Capacity == 0 then
        print("you must have at least 1 capacity to sell.")
    else
        Capacity = 0
        print("you just sold your capacity")
        print(script.Parent.Parent)
    end
end)

-- the leaderboard script

local players = game:GetService('Players')

players.PlayerAdded:Connect(function(player)
    if player then
        local folder = Instance.new('Folder')
        folder.Name = 'leaderstats'
        folder.Parent = player
        local gold = Instance.new('IntValue')
        gold.Name = 'Caps'
        gold.Parent = folder
    end
end)


Solution 1:[1]

Not sure if this is the best way to do it, but it works. I combined the sell and first script into a LocalScript inside of the tool. I did not change the leaderboard script. the tool LocalScript:

local MaxCapacity = 5 --sets the maximum amount of times you can click. (can change)
local Capacity = 0 --the default capacity (dont change)


script.Parent.Activated:Connect(function()
    if Capacity < 5 then -- change the number to your max capacity
        Capacity = Capacity + 1
        game.Players.LocalPlayer.leaderstats.Caps.Value += 1
        print(Capacity)
    else
        if Capacity == 5 then -- change the number to your max capacity
            print("you have reached max capacity, go sell it.")
        end
    end
end)

game.Workspace["sell part"].ClickDetector.MouseClick:Connect(function()
    if Capacity == 0 then
        print("you must have at least 1 capacity to sell.")
    else
        Capacity = 0
        print("you just sold your capacity")
        game.Players.LocalPlayer.leaderstats.Caps.Value = 0
        print(script.Parent.Parent)
    end
end)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nezzled