New Leaderstats Help

I keep on looking at tutorials on YouTube in order to help me learn scripting. However, in many of the tutorials it creates a new leaderstat instead of using a pre-existing one. Here is what I mean by this:

game.Players.PlayerAdded:Connect(function(plr)

local folder = Instance.new(“Folder”)
cash.Name = “leaderstats”
folder.Parent = plr

local cash = Instance.new(“IntValue”)
cash.Name = “Cash”
cash.Value = 1000
cash.Parent = folder

end)

All of the tutorials contain this, and I already have a Money leaderstat that I would like to use. I understand that I am able to just remove this but how do I then use the Money leaderstat that I already have?

3 Likes

if you have a pre-existing leaderstat folder inside the player then I would suggest using :WaitForChild(). in this example im using a. Touched event to increase the money of the player


script.Parent.Touched:Connect(function(Hit)

if Hit.Parent:FindFirstChild("Humanoid") then
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)

local leaderstat = Player:WaitForChild("leaderstats")
leaderstat.Cash.Value = leaderstat.Cash.Value + 10
end

end)

3 Likes

If you mean pre-making the leaderstats instead of creating the leaderstats folder and its values with code, you can just set it up like this:

image

And clone the folder on .PlayerAdded like so:

game.Players.PlayerAdded:Connect(function(Player)
    local leaderstats = script.leaderstats:Clone()
    
    leaderstats.Coins.Value = 1000
    leaderstats.Gold.Value = 10
    leaderstats.Exp.Value = 0
    
    leaderstats.Parent = Player
end)

I’m not quite sure what you meant, so I’m just providing a solution for this as well.

4 Likes

Just a little mistake instead of putting folder.Name you put cash.Name

Oops i read it wrong i need to read before ^^ ’

This video ^ paired with the gui tutorial series below should teach you everything you’re asking and then a lot more ontop

1 Like

Thanks, I am pretty comfortable with creating GUI’s, it’s more of the leaderstat part that’s the problem. I’ll check out that video.

If you already have a money leaderstat, then you don’t even need the script you posted?? It does the same exact thing, create a leaderstats folder.

Did you mean that you wanted the data to save once the player leaves? If so, you have to use the DataStore service which isn’t the same as leaderstats.

Otherwise, I’m pretty sure you didn’t properly understand what the event connection is doing. The code inside the function runs for each player that joins, and only once for that player. It just creates the folder when they join and does nothing else afterwards.

The reason why it’s inside Players.PlayerAdded:Connect() is because that allows you to easily run it for each player that joins using an event instead of repeatedly checking for new players in a loop.

I already have a save system - the script shown is an example of what is in every script, creating a leaderstat. I am asking help regarding the use of locating the pre-existing one and using it in the script.

1 Like

Not sure what you’re trying to say, using the line of code below, you can add 5 cash to whatever play you decide from any script. If you want to make leaderstats without scripting, I don’t believe you can. If this isn’t the answer you’re going to have to change how you word this, it makes no sense. Maybe you mean how do you access data from a datastore?

plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value + 5

You don’t need to locate the pre-existing one in the new script. Just use the old script and add whatever you want to it.

That isn’t what I mean. This current script adds a new leaderstat, I don’t want to do that.

1 Like