Troubles with leaderstats

What do you want to achieve?
I want to make a script that create a leaderstat with a custom name and that when the player join the leaderstat value is custom too. Of course it could be changed per others scripts. Also I don’t want the leaderstat to save.

What is the issue?
I read all the leaderstat’s page of the creator documention, but I don’t understand how the script that I want would look.

What solutions have you tried so far?
I found some things about leaderstats but it’s not what I want.

This script will help me for a lot of situations, and every help you give will make me a better developer than before! Thanks!

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(plr)
	local Leaderstats = Instance.new("Folder") -- create the leaderstats folder
	Leaderstats.Name = "leaderstats" -- MUST be lowercase
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash" -- change name here
	Cash.Value = math.random(0,100) -- random value between 0 and 100
	Cash.Parent = Leaderstats -- make sure the value is in the folder
	
	Leaderstats.Parent = plr -- parent it to the player so it can be visible
end)

I don’t know what you are trying to achieve but this should help you out.

1 Like

just that line I think it’s noy what I want because I don’t want to the leaderstat to have a random value of 1 to 100, I want it to have a value that I choose.

If I replace math.random(0,100) per "LeaderstatValueHere", will it works?

Yes, it will work, assuming that LeaderstatValueHere is an Integer.

1 Like

What is an Integer?

Sorry I’m still a noob in devlopment…

Integer (or Int) is a whole number, thats it,

Examples: 1, 100, 256


If you ever hear a Float:

Float (Or Floating Point Number) is a Number with Decimals

Examples: 3.14159, 1.01, 4.5


So in the Use case between an IntValue and a NumberValue, Just remember that an IntValue only Stores Whole Numbers while a NumberValue can store Decimals.

1 Like

In Roblox, these are also called Doubles.

1 Like

Does this matter? Almost nobody calls it a Double, its usually always Float

1 Like

Couldn’t add anything else to my sentence there, but that’s how Roblox references it. I also do use it. Not important, for future usage though.

1 Like

Hum ok, so if the Leaderstat value is a word, it wouldn’t work?

No, because assigning a string to a value other than an StringValue will error. You will have to change the Instance that you are creating. This is the place where you change it:

1 Like

So what would be the final script, if the Leaderstat value is a word?

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(plr)
	local Leaderstats = Instance.new("Folder") -- create the leaderstats folder
	Leaderstats.Name = "leaderstats" -- MUST be lowercase
	
	local Cash = Instance.new("StringValue")
	Cash.Name = "Cash" -- change name here
	Cash.Value = "CashValue" -- a string
	Cash.Parent = Leaderstats -- make sure the value is in the folder
	
	Leaderstats.Parent = plr -- parent it to the player so it can be visible
end)
1 Like

Thanks! I just change that ↓ ?

I’ve modified a bit the code of @SomeFedoraGuy to fit with your case :

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(plr)
	local Leaderstats = Instance.new("Folder") -- create the leaderstats folder
	Leaderstats.Name = "leaderstats" -- MUST be lowercase
	
	local Value = Instance.new("IntValue")
	Value.Name = "LeaderstatsValueHere" -- change name here
	Value.Value = 0 -- your value
	Value.Parent = Leaderstats -- make sure the value is in the folder
	
	Leaderstats.Parent = plr -- parent it to the player so it can be visible
end)

Have a nice day !

1 Like

Yes, visually for the player. You can watch tutorials on YouTube on how leaderstats are done.

1 Like

Hello! I’ve modified a bit of your code to fit fit with your issue.
Here is the code.

game.Players.PlayerAdded:Connect(function(plr)
    -- leaderstats
    local leaderstats = Instance.new("Folder") -- creates the folder
    leaderstats.Parent = plr -- places it as a child of the player
    leaderstats.Name = "leaderstats" -- names the folder    

    --Deaths
    local deaths = Instance.new("IntValue") -- creates the integer value
    deaths.Parent = leaderstats -- places it as a child of leaderstats
    deaths.Name = "Deaths" -- names the value
    if plr.Character then -- if the player has a character
        plr.Character.Humanoid.Died:Connect(function() -- if the character died
            deaths.Value = deaths.Value+1 -- adds 1 more death to the value
        end
    end

    --Group1
    local group1 = Instance.new("IntValue") -- creates the value for group1
    group1.Name = "Group1" -- CHANGE "Group1" TO YOUR GROUP NAME
    group1.Parent = leaderstats
    group1.Value = plr:GetRoleInGroup(Group1) -- CHANGE "Group1" TO YOUR GROUP ID

    --Group2
    local group2 = Instance.new("IntValue") -- creates the value for group1
group2.Name = "Group2" -- CHANGE "Group2" TO YOUR GROUP NAME
group2.Parent = leaderstats
group2.Value = plr:GetRoleInGroup(Group2) -- CHANGE "Group2" TO YOUR GROUP ID

end)

I added kills and deaths, but you can rename it to whatever you want.
If you found my answer helpful, be sure to mark it as the solution! :white_check_mark:

1 Like

I don’t want the value to be a number, but a word.

Maybe your script work for a word but I’m not sure because:

And 0 is a number.

Have a nice day too, anyways!

I’m sure this will help a lot for my future script, but that’s not what I need actually.

Thanks anyways!

Ok, thank you very much for your help!!

Have a great day!

1 Like