Well, for your levelling system, you want to use leaderstats. These are used in many games to mainly store data and see how much data you want. You would want to get a leaderstats script first in order to have data.
It's fairly simple to make a leaderstats script.
Here's an example:
I'll be going off the data type as "Levels" but you can change it later on, I suggest also looking at https://developer.roblox.com/en-us/articles/Leaderboards for more info.
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
local stats = Instance.new("IntValue")
stats.Name = "leaderstats"
stats.Parent = Player
local Levels = Instance.new("IntValue")
Levels.Name = "Levels"
Levels.Value = 0
Levels.Parent = stats
end)
That is a fairly simple leaderstats script. Now, we have the leaderstats setup, however, it's not level up the players just yet. To do that, instead of using a lot of lines you can just use a wait function of 5 minutes (300 seconds) to add 1 level. So for this, we can use a while true do function. Keep in mind there are better ways to do this! But this is a very basic one for example purposes.
So from that, we can add on to our leaderboards script.
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
local stats = Instance.new("IntValue")
stats.Name = "leaderstats"
stats.Parent = Player
local Levels = Instance.new("IntValue")
Levels.Name = "Levels"
Levels.Value = 0
Levels.Parent = stats
while true do
wait(300)
Levels.Value = Levels.Value + 1
end
end)
Now that we have a very clean and easy script, it will now add one level every 5 minutes, with just 3 lines of code. Now that we have that done, we still have an issue. The leaderboard will not save its stats, and, the GUI will also not be showing the levels.
We'll get onto the levels, and to do that, since we have already called the Player function, it makes it easier for us to get the PlayerGui from a ServerScript, and then add a line to tell the server to add a level to your GUI.
Now, I am quite unsure what the name of your GUI is, so I'm going to name it LevelGUI.
Here's an example:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
wait()
local PlayerGui = Player:WaitForChild("PlayerGui")
local GUI = PlayerGui:WaitForChild("LevelGUI")
local LevelBillboard = GUI.BillboardGui
local LevelText = LevelBillboard.TextHolderAgain.TextItSelf.Text
local stats = Instance.new("IntValue")
stats.Name = "leaderstats"
stats.Parent = Player
local Levels = Instance.new("IntValue")
Levels.Name = "Levels"
Levels.Value = 0
Levels.Parent = stats
while true do
wait(300)
Levels.Value = Levels.Value + 1
LevelText = Levels.Value
end
end)
From that, we now have the leaderstats setup, the leaderstats now add levels, and, it now shows on the GUI above the player's head. Now we have our last thing to do, and it's setting up a datastore for the game to save the levels.
From that, you can refer to https://developer.roblox.com/en-us/articles/Data-store I recommend reading it. Now, I'm personally not very good at datastores, so I'll be using a very simple distributor script made by Ziffixture (this program is not the original version; the logic was translated into the code I wrote for you below). This Datastore script will save the leaderboards every time a player exits, and when a player joins, it will load their stats.
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetGlobalDataStore("leaderstats")
local LoadedStats = {}
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Player:WaitForChild("leaderstats")
if Player.UserId > 0 and Player.Parent then
local LeaderstatsData = DataStore:GetAsync(Player.UserId)
if LeaderstatsData ~= "Request rejected" then
if LeaderstatsData then
for _, Stat in ipairs(Leaderstats:GetChildren()) do
local Value = LeaderstatsData[Stat.Name]
if Value then
Stat.Value = Value
end
end
end
LoadedStats[Player] = true
end
end
end)
Players.PlayerRemoving:Connect(function(Player)
local Leaderstats = Player:FindFirstChild("leaderstats")
if Leaderstats then
if LoadedStats[Player] then
local LeaderstatsData = {}
for i, stat in ipairs(Leaderstats:GetChildren()) do
LeaderstatsData[stat.Name] = stat.Value
end
DataStore:SetAsync(Player.UserId, LeaderstatsData)
end
else
Instance.new("Folder", Player).Name = "leaderstats"
end
LoadedStats[Player] = nil
end)
Keep in mind, that is not the best script you could use, and there are definitely much better ways to use it, but it works.
And with that, we now have a working LevelUp system that works with leaderstats and GUI.
I hope this helped you!
1 comment
For repetitive functions, use a for loop.