Search code examples
luaroblox

Roblox leaderstat not loading in


So I need five leaderstats for my game, I am able to load four of them but for some reason, I can't load in the deaths leaderstat. This is my code:

type void = nil

local function addstat(leaderstats: Folder, statname: string, stattype: string): void
    assert(leaderstats:IsA("Folder"), `Invalid argument 1 to 'addstat': Expected Folder, got {if typeof(leaderstats) == "Instance" then leaderstats.ClassName else typeof(leaderstats)}`)
    assert(typeof(statname) == "string", `Invalid argument 2 to 'addstat': Expected string, got {if typeof(statname) == "Instance" then statname.ClassName else typeof(statname)}`)
    assert(typeof(stattype) == "string", `Invalid argument 3 to 'addstat': Expected string, got {if typeof(stattype) == "Instance" then stattype.ClassName else typeof(stattype)}`)
    local success: bool, stat: ValueBase = pcall(function()
        return Instance.new(stattype);
    end)
    assert(success, "Error adding stat: Failed to create instance, invalid type?");
    assert(stat:IsA("ValueBase"), "Error adding stat: Stat type must be a Value!");
    
    stat.Name = statname;
    stat.Parent = leaderstats;
end

game.Players.PlayerAdded:Connect(function(player: Player): void
    local leaderstats: Folder = Instance.new("Folder");
    
    addstat(leaderstats, "CryptoCoin", "NumberValue");
    addstat(leaderstats, "GemCoins", "NumberValue");
    addstat(leaderstats, "Wins", "NumberValue");
    addstat(leaderstats, "Kills", "NumberValue");
    addstat(leaderstats, "Deaths", "NumberValue");

    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player;
end)

I want the code to load in all five leaderstats but for some reason it loads in only four leaderstats. There are also no errors in the output widget.


Solution

  • This is because currently the user list only accepts up to 4 Leaderstats values. The value is still be created and is still accessable through scripts. Your options are either:

    • Use less leaderstat values.
    • Create a custom leaderboard, this is what I would recommend if you need these stats to be visible to the player and are willing to spend a reasonable amount of time on a project.

    I hope this has been useful.