How to merge these 2 scripts?

So I have a script in ServerScriptService, and it makes a leaderboard with a value and so and so.

I was wondering, if I have a part that I want to give a value on touch, how would I make it add +1 to my value in ServerScriptService?

btw here is my script

game.Players.PlayerAdded:Connect(function(player) 
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats" 

	local money = Instance.new("IntValue", leaderstats) 
	money.Name = "Silver" 
	money.Value = 0 

    
	local emeralds = Instance.new("IntValue", leaderstats)
emeralds.Name = "Emeralds"
	emeralds.Value = 0 --How would I make the part add +1 emerald?
end)

To make a part add +1 emerald when touched, you can use a script in the part’s parent to detect the touch event and then update the player’s “Emeralds” value in their leaderstats. Here’s how you can do it:

  1. Insert a script into the part’s parent (for example, Workspace) or directly into the part itself. Here’s an example of the script that could be placed in the part’s parent:

    local part = script.Parent  -- Assuming the script is a child of the part
       local function onTouched(hit)
          local character = hit.Parent  -- The part that touched the object
            local player = game.Players:GetPlayerFromCharacter(character)
    
    if player then
      local leaderstats = player:FindFirstChild("leaderstats")
     if leaderstats then
         local emeralds = leaderstats:FindFirstChild("Emeralds")
         if emeralds then
             emeralds.Value = emeralds.Value + 1
             -- You can add more logic here, such as sound effects or removal of the part.
         end
     end 
    
    part.Touched:Connect(onTouched)
    

Just reference the .Value and add.

Code:

player.Silver.Value += 1

thanks! but is there a way to make that work when a textbutton is clicked?

You could use remote events.

30303030303030

If you are talking about a ScreenGui, then I believe you will have to fire a remote event to the server(correct me if i’m wrong)

--Put this script under your textbutton
script.Parent.MouseButton1Click:Connect(function()
	YourRemoteEvent:FireServer()
end)

The server script:

YourRemoteEvent.OnServerEvent:Connect(function(player)
	player:FindFirstChild("leaderstats").Silver.Value += 1
end)

Edit: I think that this should also work on SurfaceGuis

Why did you use ChatGPT to make that?