Gpt leaderboard

 avatar
MatsProg
lua
a year ago
3.8 kB
1
Indexable
Never
Sure, I can show you an example of an optimized server script in Roblox. This script is a simple leaderboard system that uses some of the optimization techniques mentioned before. It uses RemoteEvents to communicate with the clients, ReplicatedStorage to store the leaderboard data, and ContentProvider to preload the assets. It also uses local variables, functions, and events to organize the code and avoid unnecessary calculations. Here is the script:

-- Create a RemoteEvent for updating the leaderboard
local UpdateLeaderboardEvent = Instance.new("RemoteEvent")
UpdateLeaderboardEvent.Name = "UpdateLeaderboard"
UpdateLeaderboardEvent.Parent = game.ReplicatedStorage

-- Create a folder for storing the leaderboard data
local LeaderboardFolder = Instance.new("Folder")
LeaderboardFolder.Name = "Leaderboard"
LeaderboardFolder.Parent = game.ReplicatedStorage

-- Create a table for storing the players and their scores
local Players = {}

-- Create a function for sorting the players by their scores
local function SortPlayers()
    table.sort(Players, function(a, b) return a.Score > b.Score end) -- sort in descending order
end

-- Create a function for updating the leaderboard data
local function UpdateLeaderboard()
    -- Clear the previous data
    LeaderboardFolder:ClearAllChildren()

    -- Loop through the sorted players
    for i, player in ipairs(Players) do
        -- Create a new value for each player
        local value = Instance.new("IntValue")
        value.Name = player.Name
        value.Value = player.Score

        -- Set the value's parent to the leaderboard folder
        value.Parent = LeaderboardFolder
    end

    -- Fire the remote event to update the clients
    UpdateLeaderboardEvent:FireAllClients()
end

-- Create a function for adding a new player to the table
local function AddPlayer(player)
    -- Create a new entry for the player
    local entry = {
        Name = player.Name,
        Score = 0 -- default score is 0
    }

    -- Insert the entry to the table
    table.insert(Players, entry)

    -- Update the leaderboard
    UpdateLeaderboard()
end

-- Create a function for removing a player from the table
local function RemovePlayer(player)
    -- Loop through the table to find the player's entry
    for i, entry in ipairs(Players) do
        if entry.Name == player.Name then -- if the names match
            -- Remove the entry from the table
            table.remove(Players, i)

            -- Update the leaderboard
            UpdateLeaderboard()

            -- Break out of the loop
            break
        end
    end
end

-- Create a function for changing a player's score
local function ChangeScore(player, amount)
    -- Loop through the table to find the player's entry
    for i, entry in ipairs(Players) do
        if entry.Name == player.Name then -- if the names match
            -- Add or subtract the amount from the score
            entry.Score = entry.Score + amount

            -- Sort the players by their scores
            SortPlayers()

            -- Update the leaderboard
            UpdateLeaderboard()

            -- Break out of the loop
            break
        end
    end
end

-- Preload all assets in ReplicatedStorage to reduce loading times
game:GetService("ContentProvider"):PreloadAsync(game.ReplicatedStorage:GetDescendants())

-- Connect a function to the PlayerAdded event of Players service
game:GetService("Players").PlayerAdded:Connect(AddPlayer)

-- Connect a function to the PlayerRemoving event of Players service
game:GetService("Players").PlayerRemoving:Connect(RemovePlayer)

-- Connect a function to an event that changes a player's score (for example, when they collect a coin)
game.Workspace.Coin.Collected:Connect(ChangeScore)