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
8 years ago
Edited 8 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!


01local DSService = game:GetService('DataStoreService'):GetDataStore('xxxxxxxxxxxxxx')
02 
03-- Variables
04local uniquekey = "id-"..plr.userId
05local leaderstats = Instance.new('IntValue',plr)
06local abilities = Instance.new('IntValue',plr)
07local colvalue = Instance.new('IntValue')
08local lvlvalue = Instance.new('IntValue')
09local strvalue = Instance.new('IntValue')
10local convalue = Instance.new('IntValue')
11local agivalue = Instance.new('IntValue')
12local intvalue = Instance.new('IntValue')
13 
14leaderstats.Name =  'leaderstats'
15lvlvalue.Parent = leaderstats
View all 66 lines...

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 — 8y
0
oh thank you Morficc 36 — 8y

2 answers

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

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

1local colvalue = Instance.new("IntValue",leaderstats)
2    Trophies.Name = "Col"
3    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.

1if GetSaved then
2--GetSaved
3else
4local attributeSaving = {colvalue.Value,lvlvalue.Value,strvalue.Value,convalue.Value,agivalue.Value,intvalue.Value}
5 
6local DSService:SetAsync(uniquekey,attributeSaving)
7end

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 — 8y
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 — 8y
Ad
Log in to vote
0
Answered by
Morficc 36
8 years ago
Edited 8 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:

01local DSService = game:GetService('DataStoreService'):GetDataStore('xxxxxxxxxxxxxx')
02 
03game.Players.PlayerAdded:connect(function(plr)
04 
05-- Variables
06local uniquekey = "id-"..plr.userId
07local leaderstats = Instance.new('IntValue',plr)
08leaderstats.Name =  'leaderstats'
09 
10local lvlvalue = Instance.new("IntValue",leaderstats)
11    lvlvalue.Name = "lvl"
12  lvlvalue.Value = 1
13local colvalue = Instance.new("IntValue",leaderstats)
14    lvlvalue.Name = "Col"
15  colvalue.Value = 0
View all 54 lines...

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:

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

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

Answer this question