It’s extremely beginner friendly and easy to use, here’s how it works:
LSService:Create(
player, --// The player you want to give the leaderstat
"Cash", --// The name of your leaderstat
"IntValue", --// The class of your value (IntValue, StringValue, ect.)
100 --// Value of your leaderstat [OPTIONAL: Defaults to 0]
)
And - it also comes with a delete function! (if you need to delete your leaderstat)
Here’s how that works:
LSService:Delete(
player, --// The player that owns the leaderstat
"Cash" --// The name of the leaderstat.
)
Extra Information
It comes with server sided output logging, like this: Created new leaderstat for fvazer: Cash
Delete leaderstat ‘Cash’ for fvazer
These can be disabled by entering the module and setting debugmode to false at the top.
Error output logging cannot be disabled.
Get it!
Roblox
Get the module here and insert it into ServerStorage.
Pretty interesting. What features will you add to this that will make it more viable to use rather than creating the basic leaderstats script like in the beginning of the post?
local Contructor = LSService.new()
game.Players.PlayerAdded:Connect(function(player)
Constructor.make(player) {
["Cash"] = {
Value = 1000,
Type = "IntValue"
}
}
end)
Simpler
--Just making an array, calling a function that was returned, and making that array an argument for the function. Would even promote organization because you can have a template variable.
local Contructor = LSService.new()
game.Players.PlayerAdded:Connect(function(player)
Constructor.make(player)({
["Cash"] = {
Value = 1000,
Type = "IntValue"
}
})
end)
Well, that’s my two cents. Nice job on this module.
Even though I could create a module that does this in a matter of 10 minutes, no noob could, so I appreciate this module for making noob’s developing experience easier.
If this module included data saving, I was instantly use it for all my projects that need saving leader stats.
Also, how would I use the :EditValue method in a script that doesn’t have the leaderstat defined? For example, if I had a server script that was this:
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
plr.leaderstats.Bobux.Value += 1 --// How would I use :EditValue here?
end
end
OOP wouldn’t work regardless, your method would work the best, but you would need to have a table of all leaderstats if you were to do that.
Even with the :EditValue method, it would probably be the same amount of bytes as just doing it the old fashioned way.
Still, a pretty good fix.
or, what you could do, for edit value, is look through the player’s leaderstat folder, find the value with the same name (or id) as the one in the second parameter of :EditValue method, which is probably a better way to do it.
Speaking of IDs, you could give each leaderstat an ID, and you can reference it later.
I have a minor suggestion, not sure if you’re 100% up to doing this, but you could incorporate a function called .CreateLeaderstatFromTable(TableName) and it would loop through the table of specified names with their starting amounts, etc. That way, all someone has to do is create one table, use the batch function that was created to save them extra lines of code.
There is definitely better ways of doing this but that is probably the most beginner friendly one I got.
I forgot to mention use case, it’s so you don’t need to spam :Create so many times.
Fixed your code. Next time don’t use abreviations.
local debugmode = true
--// Leaderstat Service
--// Created on 13/11/2021 by Endernymous edited by chainreactionist on 20/11/2021
local module = {}
function module:Create(Player: Player, Name: string, Type: string , Value: Instance| string | number | Ray | boolean | CFrame | BrickColor | Color3 | Vector3)
if Player:FindFirstChild("leaderstats") then
-- already exists
local instance = Instance.new(Type)
instance.Name = Name
instance.Value = Value or 0
instance.Parent = Player:FindFirstChild("leaderstats")
if debugmode then
print("? Created new leaderstat for "..Player.Name..": "..instance.Name)
end
return instance
else
-- doesn't exist
local lsfolder = Instance.new("Folder")
lsfolder.Name = "leaderstats"
lsfolder.Parent = Player
print("? Created "..Player.Name.."'s 'leaderstats' folder.")
local instance = Instance.new(Type)
instance.Name = Name
instance.Value = Value or 0
instance.Parent = Player:FindFirstChild("leaderstats")
if debugmode then
print("? Created new leaderstat for "..Player.Name..": "..instance.Name)
end
return instance
end
end
function module:Delete(Player, Name)
if Player:FindFirstChild("leaderstats") then
if Player.leaderstats:FindFirstChild(Name) then
Player.leaderstats[Name]:Destroy()
if debugmode then
print("? Deleted leaderstat '"..Name.."' for "..Player.Name)
end
else
warn("? Failed to delete leaderstat for "..Player.Name..": User has no leaderstat called "..Name)
end
else
warn("? Failed to delete leaderstat for "..Player.Name..": User has no 'leaderstats' folder.")
end
end
return module
Didn’t test it tho if u run into issues be sure to let me know