EZStats: a leaderstats wrapper

Hello everyone! ヽ(>∀<☆)ノ
So I present to you my first public module ever: EZStats!

What does it do?


Well… It’s a wrapper for leaderstats!!
You create a new instance of the wrapper, and from there you can modify stuff!

Why should I use it over manually creating leaderstats?


In my opinion, it is more simple than manually doing it, and also consistent if you’re using a framework. if you don’t, try it it out! It helped me with my procrastination which was the result of code structure design. I dunno if you think the same though. ( ̄▽ ̄)

API


  • EZStats.new(player: Player)
    Description: Instantiates a copy of EZStats
    → Returns a copy of EZStats
    • _lead: the leaderstats Folder
  • EZStats:Create(valueType: string, valueName: string)
    Description: Creates a ValueBase of type valueType, with Name valueName
    valueType can be integer, number and string, case insensitive (that means you can put “iNTegEr”)
    → nil
  • EZStats:ListenTo(valueName: string, eventName: string, callback: function
    Description: Adds event listener callback to event eventName of value valueName
    → RBXScriptConnection
  • EZStats:Set(valueName: string, value: any)
    Description: Sets Value of valueName to value. If Value doesn’t accept type of value, a warning will be throwed.
  • EZStats:Get(valueName: string)
    Description: Gets the Value of valueName
    Value of valueName
  • EZStats:DestroyValue(valueName: string)
    Description: Calls Destroy on valueName
    → nil

How to use


To use EZStats, you have to require the module, then create new instances of it on the .PlayerAdded event and cache it. Here’s an example:

-- Require EZStats
local EZStats = require(PATH_TO_EZSTATS)

-- Cache for players
local plrs = {}

-- PlayerAdded event
game.Players.PlayerAdded:Connect(function(plr: Player)
	-- Cache a new EZStats instance
	plrs[plr.UserId] = EZStats.new(plr)
	-- Index the instance
	local plrLead = plrs[plr.UserId]
	-- Create an IntValue named "Cash"
	plrLead:Create("integer", "Cash")
	-- Listen to the .Changed event
	local conn
	conn = plrLead:ListenTo("Cash", "Changed", function(newVal)
		print("Detected change: " .. newVal) -- "Detected change: 20"
		conn:Disconnect()
	end)
	-- Print out value of "Cash"
	print(plrLead:Get("Cash")) -- 0
    -- Set "Cash" to 20
	plrLead:Set("Cash", 20)
	print(plrLead:Get("Cash")) -- 20
	
	plrLead:Set("Cash", 19)
    -- Destroy the value (you probably won't do this in your game)
	plrLead:DestroyValue("Cash")
end)

-- Remove player from cache (Microsoft doesn't hire people that introduce memory leaks!)
game.Players.PlayerRemoving:Connect(function(player: Player)
    plrs[player.UserId] = nil
end)

Can I access it from client?


Yup! Internally everything is a BaseValue (or else the leaderstats won’t appear, won’t they?) so you can access it via LocalPlayer.leaderstats.valueName. Or you can manually replicate the EZStats instance from the player’s cache (you don’t even need to do this if you’re using a framework!).

Where to get it


Roblox model
Source on GitHub

fun fact: most of the code is just checking the arguments

10 Likes

Omg yes!! Thanks so much for this!

1 Like

Looks useful! Just a note, I notice there’s a lot of this assertion preliminating functions in the code:

Might improve readibility if you used the assert global to do this stuff

image

2 Likes

I actually didn’t use assert because silly me thought it didn’t give a stack traceback… I just tested now and it does! I’m changing it (⌒‿⌒)

2 Likes