buttonluarobloxleaderboard

How to add 1 to the leaderboard everytime this button is clicked?


I am trying to make a script where when you click this button, the leaderboard adds 1. This is what i got so far:

local Players = game:GetService("Players")

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

    local gold = Instance.new("IntValue")
    gold.Name = "Gold"
    gold.Value = 0
    gold.Parent = leaderstats
    
    local function upd()
        gold.Value = gold.Value + 1
    end
    
    game:GetService("StarterGui").ScreenGui.TextButton.MouseButton1Click:Connect(upd)
    
end

Players.PlayerAdded:Connect(leaderboardSetup)

Thanks alot!


Solution

  • The code you used is grabbing the StarterGui object. That object is cloned into the PlayerGui of the player.

    To properly connect this, create a remoteEvent inside of ReplicatedStorage and name it "GoldClick"

    In ServerScriptService use a script like this

    local Players = game:GetService("Players")
    
    local function leaderboardSetup(player)
        local leaderstats = Instance.new("Folder")
        leaderstats.Name = "leaderstats"
        leaderstats.Parent = player
    
        local gold = Instance.new("IntValue")
        gold.Name = "Gold"
        gold.Value = 0
        gold.Parent = leaderstats
        
        local function upd()
            gold.Value = gold.Value + 1
        end
        game.ReplicatedStorage.GoldClick.OnServerEvent:Connect(function(Localplayer)
            if player.Name == Localplayer.Name then
               upd()
            end
        end)
    end
    
    Players.PlayerAdded:Connect(leaderboardSetup)
    

    Then, create a LocalScript inside of the button and use the code below

    script.Parent.MouseButton1Click:Connect(function()
    game.ReplicatedStorage.GoldClick:FireServer()
    end)