Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I optimize my DataStore script?

Asked by
Morficc 36
7 years ago
Edited 7 years ago

Hey guys! I am currently making my first game. I originally started by importing free models, plugins, scripts, etc. But I wasn't learning anything and kept getting "viruses". So I scrapped that project and started fresh, attempting to do everything myself. So far, between using the wiki, and watching youtube videos, I have been able to hash together workable scripts. However.. they are not clean. Many redundancies, and silliness abound.

Today I have come to ask for some input. I am attempting to learn and create a datastore to store players money and lvl. I also am trying to store their abilities (Strength, Constitution, Agility, Intelligence). After watching multiple youtube videos, and reading the wiki, I have not been able to learn how to accomplish this cleanly, and correctly. I do believe what I have works, but I know that I can clean it up a ton, and use some arrays to consolidate redundancy, etc. Also, I am not sure if I am creating more tables than I need to, etc. Im pretty sure I could save all of my abilities to one table, instead of separately, but I don't really know how to accomplish this.

Could someone take a look at my script, and give me some pointers if you have time? Thanks!


local DSService = game:GetService('DataStoreService'):GetDataStore('xxxxxxxxxxxxxx')

-- Variables
local uniquekey = "id-"..plr.userId
local leaderstats = Instance.new('IntValue',plr)
local abilities = Instance.new('IntValue',plr)
local colvalue = Instance.new('IntValue')
local lvlvalue = Instance.new('IntValue')
local strvalue = Instance.new('IntValue')
local convalue = Instance.new('IntValue')
local agivalue = Instance.new('IntValue')
local intvalue = Instance.new('IntValue')

leaderstats.Name =  'leaderstats'
lvlvalue.Parent = leaderstats
colvalue.Parent = leaderstats
strvalue.Parent = abilities
convalue.Parent = abilities
agivalue.Parent = abilities
intvalue.Parent = abilities
lvlvalue.Name = 'Lvl'
colvalue.Name = 'Col'
strvalue.Name = 'Strength'
convalue.Name = 'Constitution'
agivalue.Name = 'Agility'
intvalue.Name = 'Intelligence'

-- GetAsync
local GetSaved = DSService:GetAsync(uniquekey)
if GetSaved then
    colvalue.Value = GetSaved[1]
    lvlvalue.Value = GetSaved[2]
    strvalue.Value = GetSaved[3]
    convalue.Value = GetSaved[4]
    agivalue.Value = GetSaved[5]
    intvalue.Value = GetSaved[6]
else
    local colSaving = {colvalue.Value}
    local lvlSaving = {lvlvalue.Value}
    local strSaving = {strvalue.Value}
    local conSaving = {convalue.Value}
    local agiSaving = {agivalue.Value}
    local intSaving = {intvalue.Value}
    DSService:SetAsync(uniquekey,colSaving)
    DSService:SetAsync(uniquekey,lvlSaving)
    DSService:SetAsync(uniquekey,strSaving)
    DSService:SetAsync(uniquekey,conSaving)
    DSService:SetAsync(uniquekey,agiSaving)
    DSService:SetAsync(uniquekey,intSaving)
end
end)

game.Players.PlayerRemoving:connect(function(plr)
local uniquekey = 'id-'..plr.userId
local coltable = {plr.leaderstats.Col.Value}
local lvltable = {plr.leaderstats.lvl.Value}
local strtable = {plr.leaderstats.str.Value}
local agitable = {plr.leaderstats.agi.Value}
local inttable = {plr.leaderstats.int.Value}
DSService:SetAsync(uniquekey,coltable)
DSService:SetAsyne(uniquekey,lvltable)
DSService:SetAsync(uniquekey,strtable)
DSService:SetAsyne(uniquekey,contable)
DSService:SetAsync(uniquekey,agitable)
DSService:SetAsyne(uniquekey,inttable)
end)

0
I did it for you this time, but in the future please enclose your code within a code block. https://forum.scriptinghelpers.org/topic/82/how-to-format-questions-answers-on-the-main-site Pyrondon 2089 — 7y
0
oh thank you Morficc 36 — 7y

2 answers

Log in to vote
2
Answered by
Nogalo 148
7 years ago
Edited 7 years ago

Well for starters, for a cleaner look you could do this

local colvalue = Instance.new("IntValue",leaderstats)
    Trophies.Name = "Col"
    colvalue.Value = 0

for each of the attributes.

When it comes to saving you can make a table that contains all the things you want saved.

if GetSaved then
--GetSaved
else
local attributeSaving = {colvalue.Value,lvlvalue.Value,strvalue.Value,convalue.Value,agivalue.Value,intvalue.Value}

local DSService:SetAsync(uniquekey,attributeSaving)
end

you can do the same thing for the playerremoving event

1
To add to this, you could just have a 'leaderstats' folder in the script containing all the neccessary values and clone it into the client upon joining. This would save lines 5-26 Goulstem 8144 — 7y
0
I wasn't sure how to reply to this with my new code, so I replied as an answer to this post. Sorry if this was incorrect! Morficc 36 — 7y
Ad
Log in to vote
0
Answered by
Morficc 36
7 years ago
Edited 7 years ago

@Nogalo:

Okay, I have made changes to my script based on your suggestions. It does look much cleaner and I can see how that method is much more efficient than what I had. Thank you :) However, I do have a few questions. In the top right leaderboard area, I now show 4 stats. "lvl, col, strength, constitution". I am curious as to why it only showed those values, and not agility, intelligence. Does the leaderboard area only show a max of 4 values? Secondly, in the script I set the "Value" of my abilities to 5, and the value of lvl to 1, thinking that would be the starting value. But they are all still showing up in the leaderboard area as 0. What does that value section signify?

@Goulstem:

I like the sound of that. How would I go about cloning the folder and where would I place it? Other than cleaning things up, is there any performance based impact from this method, for better or worse?

Thank you so much guys! Here is my newly revamped code, based on Nogalo's suggestions:

local DSService = game:GetService('DataStoreService'):GetDataStore('xxxxxxxxxxxxxx')

game.Players.PlayerAdded:connect(function(plr)

-- Variables
local uniquekey = "id-"..plr.userId
local leaderstats = Instance.new('IntValue',plr)
leaderstats.Name =  'leaderstats'

local lvlvalue = Instance.new("IntValue",leaderstats)
    lvlvalue.Name = "lvl"
  lvlvalue.Value = 1
local colvalue = Instance.new("IntValue",leaderstats)
    lvlvalue.Name = "Col"
  colvalue.Value = 0
local strvalue = Instance.new("IntValue",leaderstats)
    lvlvalue.Name = "Strength"
  strvalue.Value = 5
local convalue = Instance.new("IntValue",leaderstats)
    lvlvalue.Name = "Constitution"
  convalue.Value = 5
local agivalue = Instance.new("IntValue",leaderstats)
    lvlvalue.Name = "Agility"
  agivalue.Value = 5
local intelvalue = Instance.new("IntValue",leaderstats)
    lvlvalue.Name = "Intelligence"
  intelvalue.Value = 5

-- GetAsync
local GetSaved = DSService:GetAsync(uniquekey)
if GetSaved then
    colvalue.Value = GetSaved[1]
    lvlvalue.Value = GetSaved[2]
    strvalue.Value = GetSaved[3]
    convalue.Value = GetSaved[4]
    agivalue.Value = GetSaved[5]
    intelvalue.Value = GetSaved[6]
else
local assigndata = {colvalue.Value,lvlvalue.Value,strvalue.Value,convalue.Value,agivalue.Value,intelvalue.Value}
DSService:SetAsync(uniquekey,assigndata)
end
end)

game.Players.PlayerRemoving:connect(function(plr)
local uniquekey = 'id-'..plr.userId
local coltable = {plr.leaderstats.Col.Value}
local lvltable = {plr.leaderstats.lvl.Value}
local strtable = {plr.leaderstats.str.Value}
local contable = {plr.leaderstats.con.Value}
local agitable = {plr.leaderstats.agi.Value}
local inttable = {plr.leaderstats.int.Value}
local savedata = {coltable,lvltable,strtable,contable,agitable,inttable}
DSService:SetAsync(uniquekey,savedata)
end)

EDIT: I was able to answer 1 of my questions. My previous saved data was overriding the starting values. Once I renamed the datastore, all starting values displayed correctly.

also, I was tinkering around with doing something like:

local Attributes = Player.attributes:GetChildren()
    for i = 1, #Attributes do
        Player:SaveNumber(Attributes[i].Name, Attributes[i].Value)
    end

Would that be feasible? Less optimized/more optimized/roughly the same?

Answer this question