I see many different versions of “Leaderstats” variations, but which one is the best and why…
--How I make a basic leaderstats
local player = game:GetService("Players")
local function SpawnedPlayer (player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Currency = Instance.new("IntValue", leaderstats)
Currency.Name = "Coins"
Currency.Value = 0
local XP = Instance.new("IntValue", leaderstats)
XP.Name = "XP"
XP.Value = 0
end
player.PlayerAdded:Connect(SpawnedPlayer)
--How I see others doing it
-- Example 1
local player = game:GetService("Players")
local function SpawnedPlayer (player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Currency = Instance.new("IntValue", leaderstats, player)
Currency.Name = "Coins"
Currency.Value = 0
local XP = Instance.new("IntValue", leaderstats, player)
XP.Name = "XP"
XP.Value = 0
end
player.PlayerAdded:Connect(SpawnedPlayer)
--Exmaple 2
local player = game:GetService("Players")
local function SpawnPlayer (player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Currency = Instance.new("IntValue")
Currency.Name = "Coins"
Currency.Value = 0
Currency.Parent = leaderstats
local XP = Instance.new("IntValue")
XP.Name = "XP"
XP.Value = 0
XP.Parent = leaderstats
end
player.PlayerAdded:Connect(SpawnPlayer)
Example 2 is the best. Using the parent parameter (the second one) in Instance.new() can cause memory leaks. Now in Example 1, ngl I have no idea whats going on there with the 3 parameters but that is definitely not correct.
I recommend example 2 personally as specifying the 2nd parameter in an instance.new parents it immediately, which can be slow due to replication
Though I don’t think it would cause a massive slow down with just 3 instances, it’s a barely noticeable slow down, but if you do it frequently everywhere else that wouldn’t be good. Your method would still work, though most people recommend to refrain from the 2nd argument
The 1st example is just plain wrong, there’s only 2 arguments in Instance.new at most
In this case, it doesn’t cause much of a problem if you use the 2nd parameter here as it would barely cause an impact, though it’s recommended everywhere or almost everywhere that you do not use that argument, so it’s best to not use that and opt for how example 2 does it
The way this has been set out, tells me that, it’s always good practice to to declare the parameters on a seperate line , along with the part.parent. And not just for leaderstats…
It’s recommended that you assign the parent property last, so that way replication is done only once rather than every proprety change, using the 2nd argument sets the parent first, which replicates everytime you change a property
Instance.new's second parameter doesn’t cause a memory leak it just forces instances to be replicated earlier than necessary. The best approach is to parent the ‘leaderstats’ folder last.
local Game = game
local Players = Game:GetService("Players")
local function OnPlayerAdded(Player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
local Stat1 = Instance.new("IntValue")
Stat1.Parent = Leaderstats
local Stat2 = Instance.new("IntValue")
Stat2.Parent = Leaderstats
Leaderstats.Parent = Player --Parent the 'leaderstats' folder last.
end
Players.PlayerAdded:Connect(OnPlayerAdded)