Easy Leaderstats Set Up Module

Alright, this was something I made and thought of a while back, but I figured I should post it onto resources for everyone to use. It was a simple setup for leaderstats that can be used by creating tables. You can customize the value from the table, the name of a datastore, and even whether to save a specific value. So, here is the server script (which is not what you need to change)

local LInfo = require(script.LeaderstatsSetup)
local DSS = game:GetService("DataStoreService")

game.Players.PlayerAdded:Connect(function(plr)
	local data
	
	if LInfo.DataStoreName then
		data = DSS:GetDataStore(LInfo.DataStoreName):GetAsync(plr.UserId) or nil
	end
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	for i = 1, #LInfo.Values do
		local v = LInfo.Values[i]
		local NewValue = Instance.new(v.Instance)
		NewValue.Name = v.Name
		if data and v.Save then
			if data[v.Name] then
				NewValue.Value = data[v.Name]
			else
				NewValue.Value = v.StartValue
			end
		else
			NewValue.Value = v.StartValue
		end
		NewValue.Parent = leaderstats
	end
	
end)

function DataSave(plr)
	if LInfo.DataStoreName then
		local ds = DSS:GetDataStore(LInfo.DataStoreName)

		local savedata = {}

		for i,v in pairs(plr.leaderstats:GetChildren()) do
			if LInfo.Values[i].Name == v.Name and LInfo.Values[i].Save then
				savedata[v.Name] = v.Value
			end
		end

		ds:SetAsync(plr.UserId, savedata)
	end
end

game.Players.PlayerRemoving:Connect(function(plr)
	DataSave(plr)
end)

game:BindToClose(function()
	for i,v in pairs(game.Players:GetPlayers()) do
		DataSave(v)
	end
end)

And here is the module script.

local LInfo = {
	["DataStoreName"] = "TestTycoonData", --Name of datastore
	["Values"] = {
		[1] = {
			["Instance"] = "NumberValue", --Type of Value
			["StartValue"] = 0, --What the value will start out as
			["Save"] = true, --Whether to save this value's data when left
			["Name"] = "FirstValue" --Name of the value
		},
	},
}

return LInfo

The module script’s name is “LeaderstatsSetup” and is put under the server script. You simply need to customize the module to your needs. You can make more values by adding another table into the “Values” Table.

local LInfo = {
	["DataStoreName"] = "TestTycoonData", --Name of datastore
	["Values"] = {
		[1] = {
			["Instance"] = "NumberValue", --Type of Value
			["StartValue"] = 0, --What the value will start out as
			["Save"] = true, --Whether to save this value's data when left
			["Name"] = "FirstValue" --Name of the value
		},
		[2] = {
			["Instance"] = "BoolValue",
			["StartValue"] = true,
			["Save"] = false,
			["Name"] = "FirstBool"
		},
	},
}

return LInfo

You can create as many values as you want with this.
Here is the model for it: Easy Leaderstats Setup - Roblox

Feel free to ask for any sort of function or ask any further questions on how to use it!

7 Likes

Hi, how do I know what is the name of the datastore and what kind of script should I use?

1 Like

I believe I had clarified, but for more clarification, the first script would be a server script and the second would be a module script under that server script. Thanks for the reply :slight_smile:!

2 Likes

Doesn’t this seem a bit too complicated? I mean, we could just do this all in one script right?

3 Likes

Yes, but the point is to make you not have to add extra lines for this. Yes, you can do this within 1 script, but it adds a lot more lines, which will keep increasing as you add more values. With this script, you can simply edit a table within a module and it will decrease the amount of effort and work you put into scripting this. This is also mainly put in for people who aren’t exactly the best at scripting. If you are not a scripter and don’t know how to script, you can simply use this with the tutorial given.

1 Like

Not to be rude, however wouldn’t a module such as this one, be quicker, and easier to setup and require less lines of code? It’s got the bonus of supporting both DataStore2 and ProfileService saving techniques. Otherwise, I guess like-minded people think alike.

2 Likes

To be honest, I didn’t even know that there was already a module for this type of stuff, I did check the related list while typing all of this out, but that one didn’t show. Yes, yours is more advanced and has more functions compared to mine. Although mine does require less work to actually make as it requires a 5 line table, yours is better in every other regard.

1 Like

I can’t blame you for not noticing. The DevForum search is practically broken.

2 Likes

Hello! I have one question, sometimes I get the warning “DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.” Do you know how I can fix it? Thanks!

1 Like

This isn’t an error, this is just telling you that it is saving the data and it is in the queue to be saved.

2 Likes