Coin Leaderboard Issue[Fixed]

I’m making that if a coin is collected you earn a point,and this script worked in my game in the past,but for some reason it’s not working anymore,i have tried changing the name of the point and leaderstats but it simply doesn’t work.

Script:
amnt = 1
function onTouched(part)
local h = part.Parent:findFirstChild(“Humanoid”)
if (h~=nil) then
local thisplr = game.Players:findFirstChild(h.Parent.Name)
if (thisplr~=nil) then
local stats = thisplr:findFirstChild(“leaderstats”)
if (stats~=nil) then
local score = stats:findFirstChild(“Coins”)
if (score~=nil) then
score.Value = score.Value + amnt
end
end
end
script.Parent:remove()
end
end

script.Parent.Touched:connect(onTouched)

2 Likes

Is there an error?


When creating posts that involve code on the DevForum, please try to format your code so it's easier to help you. I wrote the guide below which should hopefully help you with this.
2 Likes

Are the leaderstats ever created?

[Tested]

To create a leaderboard and increment an int value ,

such as in your case “coins”, you would :

Create a Server Script in Server Script Service for the leaderboard :

local Players = game:GetService("Players")
 
 Players.PlayerAdded:Connect(function(player)--for each player , when they join
	 --create a leaderboard for them
	 local leaderstats = Instance.new("Folder")
	 local coins = Instance.new("IntValue")
	 
	   
	   leaderstats.Name = "leaderstats"
	   coins.Name = "Coins"
	
       leaderstats.Parent = player
        coins.Parent = leaderstats
 end)

Next to increase the value each time a coin is touched, an efficient method (rather than creating a separate script for each coin) would be to :

  • Create a folder in workspace called CoinsFolder

  • insert all your coins (just the parts) to the folder

  • position your coins wherever you want to

  • loop through all coins to increase the value : described below

    You would now add some line below the above code (in the same script I mean) :

          local CoinsFolder  = workspace:WaitForChild("CoinsFolder")
     
     amt = 10--how much each coin will give
     debounce = true-- so the player doesn't get hundreds of coins in one touch
     
      for _, coin in ipairs(CoinsFolder:GetChildren()) do
          coin.Touched:Connect(function(hit)
            local player = Players:GetPlayerFromCharacter(hit.Parent)
               if player and debounce then
                  debounce=false
                    local lead = player:WaitForChild("leaderstats")
                  local coins = lead.Coins
                coins.Value = coins.Value + amt
              wait(0.5)--how much time he has to wait before his coins value is increased again
          debounce=true
        end
      end)
    end
    
So in the end your complete script in Server Script Service would look like :
    local Players = game:GetService("Players")
 
     Players.PlayerAdded:Connect(function(player)
	 
	 local leaderstats = Instance.new("Folder")
	 local coins = Instance.new("IntValue")
	 
	   leaderstats.Name = "leaderstats"
	   coins.Name = "Coins"
    
    leaderstats.Parent = player
   coins.Parent = leaderstats
	
 end)

   local CoinsFolder  = workspace:WaitForChild("CoinsFolder")
   
   amt = 10
   debounce = true
   
    for _, coin in ipairs(CoinsFolder:GetChildren()) do
	    coin.Touched:Connect(function(hit)
          local player = Players:GetPlayerFromCharacter(hit.Parent)
             if player and debounce then
	            debounce=false
                local lead = player:WaitForChild("leaderstats")
                local coins = lead.Coins
              coins.Value = coins.Value + amt
            wait(0.5)
        debounce=true
      end
    end)
  end

But remember , to ensure that these values save, you would need to use Datastores

3 Likes

@XxELECTROFUSIONxX is right, but try to parent and instances after you define all the properties, as a thread I’ve read: PSA: Don't use Instance.new() with parent argument

1 Like

oh yeah! thanks for that , I guess I let that slip . Edited my reply

Perhaps you could use GetPlayerFromCharacter instead of findFirstChild @TioKirby so it would look something like this:

local thisplr = game.Players:GetPlayerFromCharacter(part.Parent)

Naming is important as well, making sure that your coins value is addressed properly is crucial.

Thanks everyone for helping but i already fixed the issue,sorry for not answering,i’m a bit inactive lately