Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

leaderstat values not being created?

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)

2 comments

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.

0

oops, didn't realize. that's a very foolish mistake

1 answer

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


This discussion is preserved as it was. New answers, comments, and votes are closed.