Leaderstats question

i have a part with a click detector in the workspace, how would i add +1 to the leaderstats when the part is clicked?

local stat = "Points"
local startamount = 0 


local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LeaderStatSave")


game.Players.PlayerAdded:connect(function(player)
 local leader = Instance.new("Folder",player)
 leader.Name = "leaderstats"
 local Cash = Instance.new("IntValue",leader)
 Cash.Name = stat
 Cash.Value = ds:GetAsync(player.UserId) or startamount
 ds:SetAsync(player.UserId, Cash.Value)
 Cash.Changed:connect(function()
  ds:SetAsync(player.UserId, Cash.Value)
 end)
end)

game.Players.PlayerRemoving:connect(function(player)
 ds:SetAsync(player.UserId, player.leaderstats.Cash.Value)
end)

You can modify here as it follows

local stat = "Points"
local startamount = 0 

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LeaderStatSave")
local clickDetector = workspace.PartWithClickDetector.ClickDetector -- Adjust this line to reference your part

-- Handle when a player joins the game
game.Players.PlayerAdded:connect(function(player)
    local leader = Instance.new("Folder", player)
    leader.Name = "leaderstats"
    
    local Cash = Instance.new("IntValue", leader)
    Cash.Name = stat
    Cash.Value = ds:GetAsync(player.UserId) or startamount
    ds:SetAsync(player.UserId, Cash.Value)
    
    Cash.Changed:connect(function()
        ds:SetAsync(player.UserId, Cash.Value)
    end)
end)

-- Handle when a player leaves the game
game.Players.PlayerRemoving:connect(function(player)
    ds:SetAsync(player.UserId, player.leaderstats[stat].Value)
end)

-- Handle when the part is clicked
clickDetector.MouseClick:connect(function(player)
    local leaderstats = player:FindFirstChild("leaderstats")
    if leaderstats then
        local points = leaderstats:FindFirstChild(stat)
        if points then
            points.Value = points.Value + 1
        end
    end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.