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)
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