How can I optimize my code for more values?

So, I’m working on a project and will be needing a lot of string, int and boolean values for my game. Question is, how can I instance all of them in one script in an efficient way?

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	
	local TorchUpgradeLevel = Instance.new("IntValue")
	TorchUpgradeLevel.Name = "TorchUpgradeLevel"
	TorchUpgradeLevel.Value = 1
	TorchUpgradeLevel.Parent = Player
	
	local TorchUpgradeLevel = Instance.new("IntValue")
	TorchUpgradeLevel.Name = "MedKitUpgradeLevel"
	TorchUpgradeLevel.Value = 1
	TorchUpgradeLevel.Parent = Player
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	
	local Voted = Instance.new("StringValue")
	Voted.Name = "Voted"
	Voted.Value = "N/A"
	Voted.Parent = Player

	local Points = Instance.new("IntValue")
	Points.Name = "Points"
	Points.Value = 500
	Points.Parent = leaderstats
	
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Value = 0
	Level.Parent = leaderstats
	
end)

I’ll be having MUCH more values than just this so I need to find a way. Is this how it works or is there a better way?

2 Likes

One way would be to have the leaderstats folder inside of the script in Studio, and have the script parent the leaderstats folder to the player. This method doesn’t require as many lines of code.

Hope this helped.

2 Likes

You can try declaring all variables first, and then change their values under. For example:

local C = Instance.new("IntValue")
local P = Instance.new("IntValue")

-------------------------------

C.Name = "Cash"
C.Value = 0
--declare after

This is how I organize my values, and it’s pretty efficient for me.

1 Like
local Players = game:GetService("Players")

-- Values

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"

local TorchUpgradeLevel = Instance.new("IntValue")
TorchUpgradeLevel.Name = "TorchUpgradeLevel"
TorchUpgradeLevel.Value = 1

local MedKitUpgradeLevel = Instance.new("IntValue")
TorchUpgradeLevel.Name = "MedKitUpgradeLevel"
TorchUpgradeLevel.Value = 1

local Voted = Instance.new("StringValue")
Voted.Name = "Voted"
Voted.Value = "N/A"

local Points = Instance.new("IntValue")
Points.Name = "Points"
Points.Value = 500

local Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Value = 0

Players.PlayerAdded:Connect(function(Player)
	
	TorchUpgradeLevel.Parent = Player
	TorchUpgradeLevel.Parent = Player
	leaderstats.Parent = Player
	Voted.Parent = Player
	Points.Parent = leaderstats
	Level.Parent = leaderstats
	
end)

This is what I accomplished, still is there any other way?

1 Like

People will probably give you more possibilities to sort, but that’s my unique way.

1 Like

Alright, I’ll use yours since that seems a good idea.

1 Like

Well, that is the most efficient way you can do it. If you want to make it shorter, you can make a function:

local function CreateValue(ValueType, ValueName, ValueValue, ValueParent)
    local NewValue = Instance.new(ValueType)
    NewValue.Name = ValueName
    NewValue.Value = ValueValue
    NewValue.Parent = ValueParent
    return NewValue
end

Players.PlayerAdded:Connect(function(Player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	
    local Voted = CreateValue("StringValue",  "Voted", "N/A", Player)
    local TorchUpgradeLevel = CreateValue("IntValue",  "TorchUpgradeLevel", 1, Player)

    -- etc.
	
end)

Basically this makes the code much cleaner and much easier to write.

3 Likes

@VegetationBush has the best solution, but a recommendation from me is to not use values and store the data inside of the script using a module. I know values are better for visualization, but doing everything inside of the script just keeps it more organized and less cluttered.

What I do is create and handle all of the player’s data from one script on the server using a module I made for stats, and then since I would only be changing ANY data on the server, all I had to do was make some bindable functions I could fire at will from any other server script to change the player’s data in the one server script. Currently I have four remote functions:

  • requestAdd — increments a player’s data
  • requestSubtract — Decrements a player’s data
  • requestChange — Changes the value of a player’s data
  • requestValue — Retrieves and returns the value of a player’s data (useful for checking, for example checking if someone has enough money to buy something)
  • requestRemove —- Removes a player’s data, havent used this much though

This is my workflow, feel free to some ideas from this. Good luck!

1 Like

I would do something like

local ToCreate = {
[1] = {Type='IntValue',Value=0,Name='Cash'}
}

and with a ipairs loop, iterate in all values of table and create!!

1 Like

Maybe it’d be a lot simpler to create them all in ServerStorage not via script, but as already existing instances in the game, then at runtime, clone and reparent them.

This is an ideal situation for just setting everything up in a Folder somewhere and cloning it into each player:

local Players = game:GetService("Players")

local defaultLeaderstats = game.ServerStorage:WaitForChild("DefaultLeaderstats")

Players.PlayerAdded:Connect(function(Player)
	local leaderstats = defaultLeaderstats:Clone()
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
end)

Code Review
That is the most efficient way to display what is going on on the leaderboard and also to contain the player’s state. Though, there is a much, much better solution than you have right now. It may not be efficient, but it’s much more effective.

Defining problem
I notice on the code example, you’re doing two things:

  1. making new values for whenever a player joins (and attach them to that player);
  2. adding certain values to the leaderboard.

It isn’t a good idea to have them attached to the Player or Leaderboard folder. It makes things too fractured for what should be in the same place. So, the problem can be divided into these following problems:

  • We want to create a new player profile.

    • We should be able to query for certain values from a player.
    • We should be able to change certain values from a player.
  • We want to also be able to display certain values to the leaderboard.

    • We should be able to add values to display.
    • We should be able to change what value is being displayed.
    • We should be able to remove values from being displayed.

Solution
Well, it’s in the outline of the problem itself. Make the following modules:

  1. a module to create new player profiles;

    Probably make it a prototype. So, make a default player profile and make a function to derive new prototypes; using the default as the prototype.

  2. a module to manage those player profiles;

    It should be a dictionary of functions that create, destroy, read, and manage player profiles. Also, add events for whenever the aforementioned happens.

  3. a module to manage the leaderboard.

    It creates a leaderboard folder in players who joined. It has a function that you can add values to, including a default value as well. It should also be able to remove a value from the leaderboard and also set values for everyone. You can use the events from the last module to change the values for the player.

Closure
I can help you make them, though, it’s your job to implement them. The solution I came up with is made to be highly scalable. There is a good chance that you aren’t looking for such scalability anyways. So, maybe look for other solutions. There might be modules that already handle this for you, I don’t know! This is more or less, a launch-pad.