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?
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.
@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!
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:
making new values for whenever a player joins (and attach them to that player);
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:
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.
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.
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.