Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do you make a leaderboard stat that increases when a part is touched?

Can someone explain how to make it? I don't want the script itself, just how to make it and how it works.

4 answers

First you’d have to understand how to make a leaderboard to begin with, this isn’t too difficult though. Essentially, we create a folder within the Player titled leaderstats to identify to the Studio we wish to build a Leaderboard, but then what? You need to store ValueObjects that will be used as footholds for the data; a leaderstat. If we’re incrementing, I assume it’s a positive integer, so we will use an IntValue. See this article to learn how to do all this.


For detecting a Player touching a part, we can use the .Touched signal of BasePart to attach a listener for such a scenario:

local Part = workspace.Part

Part.Touched:Connect(function(Hit)

end)

Hit is a parameter given to us by this signal, it represents the Instance that initiated contact. This is pretty simple, but can become complicated quickly. This signal isn’t privy to Players, it will fire to any Instance that interacts with it, so we must be carful with how we determine a Player. As we know, our Character is what’s actually touching our Part, so we will have to begin with ensuring we are dealing with one. The best was can do this is by filtering for a Humanoid:

local Part = workspace.Part

Part.Touched:Connect(function(Hit)
    local Humanoid = Hit.Parent:FindFirstChildOfClass("Humanoid")
end)

Hit will likely represent a BodyPart, so we use .Parent to get to the Model, and safe-search for a Humanoid. If we get a valid result, we can assume Hit.Parent is our Character!

After this, we can presume we have a Character, all that’s left is to connect it to it’s affiliated Player, and then find the leaderstats folder within them as I mentioned earlier. This can be achieved using a helpful method of the Players Service, :GetPlayerFromCharacter():

local Players = game:GetService("Players")

local Part = workspace.Part

Part.Touched:Connect(function(Hit)
    local Humanoid = Hit.Parent:FindFirstChildOfClass("Humanoid")
    if (Humanoid and Humanoid.Health > 0) then
        local Player = Players:GetPlayerFromCharacter(Hit.Parent)
        local Leaderstats = Player:WaitForChild("leaderstats")
        local Number = Leaderstats:FindFirstChild("Number")
        if (Number) then
            Number.Value = (Number.Value + 1)
        end
    end
end)

You should apply a debounce to ensure the incrementation doesn’t skyrocket.

Hope this helps you get an idea where to begin!

I have stuff that may work:

  1. Create leaderstats script in Workspace:

game.Players.PlayerAdded:Connect(function(p)
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = p
	local Stat = Instance.new("IntValue")
	Stat.Parent = stats
	Stat.Name = "Times Touched"
end)
  1. Put this script in the Part:

local debounce = false
 
function touch(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if debounce == false and player then
		player.leaderstats:WaitForChild("Times Touched").Value = player.leaderstats:WaitForChild("Times Touched").Value + 1
		debounce = true
		wait(1)
		debounce = false
	end
end

script.Parent.Touched:Connect(touch)

-1bitPro

#Step 2: Insert a script into ServerScriptService. This will be for the leaderboard and also insert a BindableEvent in ReplicatedStorage.

game.Players.PlayerAdded:Connect(function(player)
        
       local leaderboard = Instance.new("Folder")
       leaderboard.Name = "leaderstats"
       leaderboard.Parent = player
      
       local cash = Instance.new("IntValue")
       cash.Name = "Cash" -- Change Cash to your own preference 
       cash.Value = 0
       cash.Parent = leaderboard  

       game.ReplicatedStorage.BindableEvent.Event:Connect(function()
             player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 100 -- Change cash to your leaderboard's IntValue.
     end)

end)

#Step 2: Insert a script into the part

script.Parent.Touched:Connect(function(hit)
         game.ReplicatedStorage.BindableEvent:FireServer()
end)

Please make sure to upvote and select this as your answer if it worked!

Basically, First you create a leaderstats, then you Add a Child to it (Gold, Coins, Money, Exp etc...)

Then you do the Touch Function

part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent.leaderstats.wins.Value = hit.Parent.leaderstats.wins.Value + 1

I hope I could help maybe a little bit!

3 comments

0

leaderstats is in the player, not the character. Use "game.Players:GetPlayerFromCharacter(hit.Parent)"

0

did you downvote just because I was trying to help him?

0

That wasn't me who downvoted.

This discussion is preserved as it was. New answers, comments, and votes are closed.