Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Help with Level system over and over please?

Asked by 3 years ago
script.Parent.BillboardGui.TextHolderAgain.TextItSelf.Text = "Level 1"
wait(2)
script.Parent.BillboardGui.TextHolderAgain.TextItSelf.Text = "Level 2"
wait(2)
script.Parent.BillboardGui.TextHolderAgain.TextItSelf.Text = "Level 3"
wait(2)
script.Parent.BillboardGui.TextHolderAgain.TextItSelf.Text = "Level 4"
wait(2)
script.Parent.BillboardGui.TextHolderAgain.TextItSelf.Text = "Level 5"
wait(2)
script.Parent.BillboardGui.TextHolderAgain.TextItSelf.Text = "Level 6"
wait(2)
script.Parent.BillboardGui.TextHolderAgain.TextItSelf.Text = "Level 7"
wait(2)
script.Parent.BillboardGui.TextHolderAgain.TextItSelf.Text = "Level 8"
wait(2)
script.Parent.BillboardGui.TextHolderAgain.TextItSelf.Text = "Level 9"
wait(2)
script.Parent.BillboardGui.TextHolderAgain.TextItSelf.Text = "Level 10"
wait(2)

I am currently trying to make a overhead level system so every 5 minutes the level goes up one more aka +1 so it starts at 1 goes to 2 then 3 etc. and i also need help for keeping stored so when they rejoin its the same number. Please help me with this. this was the only code i could think of

example, The text = Level 1,Level 2 and up.

Which is repetitive and i dont know how to save the number aswell as making the script smaller and using the math for it.

0
For repetitive functions, use a for loop. iNot_here 93 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago
Edited by Ziffixture 3 years ago

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.

-- Insert into ServerScriptService as a ServerScript
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.

-- Insert into ServerScriptService as a ServerScript
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 -- for this function you would definitely want to use for loops in order to make it repeat, the reason as to why I have not used it is for example purposes.
     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:

-- Insert into ServerScriptService as a ServerScript
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.

-- ServerScriptService as a ServerScript
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!

0
That was a great answer. Ascarson4 138 — 3y
0
Thank you RazzyPlayz 497 — 3y
Ad

Answer this question