I started a new project, but only 16 datastores made my DS script a little over 250 lines. I’m using the DataStore2 = require(1936396537) module and there’s many similar line groups. What I want is to know if there’s a way to keep the script efficient, but make it shorter and easier to read by calling a function for all these similar line groups.
Example:
local fightingLevelDS = DataStore2("fightingLevel", player)
local FightingLevel = Instance.new("IntValue", Data)
FightingLevel.Name = "FightingLevel"
local function fightingLevelUpdate(updatedValue)
FightingLevel.Value = fightingLevelDS:Get(updatedValue)
end
fightingLevelUpdate(fightingLevelDefault)
fightingLevelDS:OnUpdate(fightingLevelUpdate)
local baseTalentPointsDS = DataStore2("baseTalentPoints", player)
local BaseTalentPoints = Instance.new("IntValue", Data)
BaseTalentPoints.Name = "BaseTalentPoints"
local function baseTalentPointsUpdate(updatedValue)
BaseTalentPoints.Value = baseTalentPointsDS:Get(updatedValue)
end
baseTalentPointsUpdate(baseTalentPointsDefault)
baseTalentPointsDS:OnUpdate(baseTalentPointsUpdate)
Update on the topic 24th oct. 2021
I've got a new idea to save ((9-1)+(4-1))x6 = 66 lines and more in the future, and it seems to work so I want to share it for the people who see this topic!
The idea is that I put the first two triggers (lines 228-233) inside the end of the for loop with argument ind instead of number. Here’s how it looks now:
I tried making a table for 9 datastores, but I can’t find a way to edit it. There’s no table object and to set an array of IntValues I need some sort of a string because every time I set a variable to someDS:Get() when I print it using tostring() I get table: 0xda1006956e4ae51c instead of indexes to edit.
If possible you should probably have a single datastore with 16 different entries, rather than 16 datastores with a single entry. As an example your datastore table may look like this:
local data = {
-- key for storing user's data
["user_250113"] = {
fightingLevel = 10,
baseTalentPoints = 5,
otherStat = 3,
otherStat1 = 3,
otherStat2 = 3,
otherStat3 = 3,
}
}
This would cut down on a lot of the repeated code, as you could create a generic update function which every element can use.
I’ll keep experimenting with that. Meanwhile can you tell me if it’s possible to define a variable through a string. For example instead of Instance.Value to use a string and reach that instance this way?
I’m calling a remote when the client wants to upgrade a talent, containing a string that defines which talent is the request for. Currently there’s a ton of if statements to choose the requested talent:
if talent == "STR" then
strDS:Set(STR.Value + 1, baseTalentsDefault)
baseTalentsUpdate(baseTalentsDefault)
strDS:OnUpdate(baseTalentsUpdate)
end
if talent == "AGI" then
agiDS:Set(AGI.Value + 1, baseTalentsDefault)
baseTalentsUpdate(baseTalentsDefault)
agiDS:OnUpdate(baseTalentsUpdate)
end
So I think I can use only three lines without copy-pasting if the above is possible.
Yeah, if you had a table which pointed towards your Instances you could do that, for example it could look like this:
local STR = Instance.new("IntValue")
local DEX = Instance.new("IntValue")
local AGI = Instance.new("IntValue")
local stat_map = {
STR = {STR, strDS}, -- index the table with "STR" to access these contents
DEX = {DEX, dexDS},
AGI = {AGI, agiDS},
}
function setUpdate(name)
if stat_map[name] then
local stat = stat_map[1] -- get the Value and Datastore from the array in stat_map
local ds = stat_map[2]
ds:Set(stat.Value + 1, baseTalentsDefault)
baseTalentsUpdate(baseTalentsDefault)
ds:OnUpdate(baseTalentsUpdate)
end
end
setUpdate("STR")
setUpdate("AGI")
But again this is made messier by having multiple datastores, as with a single stat datastore you could simplify it to:
local STR = Instance.new("IntValue")
local DEX = Instance.new("IntValue")
local AGI = Instance.new("IntValue")
local stat_table = {
STR = STR,
DEX = DEX,
AGI = AGI,
}
function setUpdate(name)
if stat_table[name] then
stat_table[name].Value += 1
statDS:Set(stat_table, baseTalentsDefault)
baseTalentsUpdate(baseTalentsDefault)
end
end
statDS:OnUpdate(baseTalentsUpdate) -- this should only be called once
setUpdate("STR")
setUpdate("AGI")
sorry I’m not super familiar with DS2 so I think I made some mistakes, I read the documentation a little bit and think the second code snippet should be correct now.
I’ve made some updates on the main topic, if anyone wants to add on feel free to reply!
Note: it’s solved so you don’t need to answer me, but the people that may find this helpful