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

leaderstat values not being created?

Asked by 6 years ago

I'm working on a game that obviously has leaderstats and this script has been giving me issues and if anyone knows why it is loading the value "Trimmings" and "Mower" but not the value "Strength" into the leaderstats folder in studio, please do tell me how to fix it. Script:

game.Players.PlayerAdded:connect(function(plr)
    local ls = Instance.new('Folder')
    ls.Name = 'leaderstats'
    ls.Parent = plr

    local t = Instance.new('NumberValue')
    t.Name = 'Trimmings'
    t.Parent = ls

    local s = Instance.new('NumberValue')
    s.Name = 'Strength'
    s.Parent = ls
    s.Value = 0

    local m = Instance.new('StringValue')
    s.Name = 'Mower'
    s.Parent = ls
    s.Value = 'Mower'
end)
0
I know, on the local m thing you then under put in s.name, parent and value put in m instead just a normal mistake. Nilsen84 1 — 6y
0
oops, didn't realize. that's a very foolish mistake Sonostor 17 — 6y

1 answer

Log in to vote
0
Answered by
Azarth 3141 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

Since you figured it out, you can also optimize this by using a dictionary!

local players = game:GetService("Players")

local stats = {
    ["Trimmings"] = {objectType = "NumberValue", defaultValue = 0};
    ["Strength"] = {objectType = "NumberValue", defaultValue = 0};
    ["Mower"] = {objectType = "StringValue", defaultValue = "Mower"}
}

local function playerJoined(player)
    -- create your leaderstats main
    local leaderstats = Instance.new("StringValue")
    leaderstats.Name = "leaderstats"
    -- for loop through the stats
    for index, value in pairs(stats) do 
        local stat = Instance.new(value.objectType, leaderstats)
        stat.Name = index
        -- value is the indexes table
        stat.Value = value.defaultValue
    end     
    -- parent the main last
    leaderstats.Parent = player
end

players.PlayerAdded:Connect(playerJoined)

-- gets all the players that loaded before the script did - if any
for i,v in pairs(players:GetPlayers()) do 
    playerJoined(v)
end


Ad

Answer this question