Five weeks into scripting

After 5 weeks of scripting, This is as much as I can remember in terms of creating leaderstas. Are their any ways to make this better?

--//getServices
local player = game:GetService("Players")

---------------------------------------------------------------------------------------	

--//leaderstatsFunction
player.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
---------------------------------------------------------------------------------------	
	--//Adding a "Stage" count to the leaderstats
	local Stage = Instance.new("IntValue")
	Stage.Name = "Stage"
	Stage.Value = 0
	Stage.Parent = leaderstats
	
---------------------------------------------------------------------------------------	
	
	local XP = Instance.new("IntValue")
	XP.Name = "XP"
	XP.Value = 0
	XP.Parent = leaderstats
	
---------------------------------------------------------------------------------------
	
--//Connects the MinV & MaxV to the Currency using an intValue.
	local MinV = game.ReplicatedStorage.CurrencyValue:GetAttribute("MinV")
	local MaxV = game.ReplicatedStorage.CurrencyValue:GetAttribute("MaxV")
	
	local Currency = Instance.new("IntValue")
	Currency.Name = "Coins"
	Currency.Value = MinV
	Currency.Parent = leaderstats
	
	Currency.Changed:Connect(function()
	Currency.Value = math.clamp(Currency.Value, MinV, MaxV)
		
	end)	
end)
2 Likes

Instead of making a whole other line of code for parenting the IntValue, try this:

Instance.new(“IntValue”, leaderstats)

No, that is less performant PSA: Don't use Instance.new() with parent argument

And for the script, everything looks fine although I’m not that experienced with leaderstats.

5 Likes

When it comes to comments, you should be commenting why not what. Eg, it is easy to see that you are creating an integer value called Stage, but it would be more meaningful to explain why you are doing so.

2 Likes

I used to do it like that, but I had several people tell me not to as it causes performance issues.

2 Likes

Instead of doing Currency.Changed:Connect()
I would recommend using Currency:GetPropertyChangedSignal("Value"):Connect() as it only fires when the value changes instead of any property change.

I would also recommend to rename the variable for the players service to “Players” to make it different from the parameter name.