Answered by
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:
01 | local DSService = game:GetService( 'DataStoreService' ):GetDataStore( 'xxxxxxxxxxxxxx' ) |
03 | game.Players.PlayerAdded:connect( function (plr) |
06 | local uniquekey = "id-" ..plr.userId |
07 | local leaderstats = Instance.new( 'IntValue' ,plr) |
08 | leaderstats.Name = 'leaderstats' |
10 | local lvlvalue = Instance.new( "IntValue" ,leaderstats) |
13 | local colvalue = Instance.new( "IntValue" ,leaderstats) |
16 | local strvalue = Instance.new( "IntValue" ,leaderstats) |
17 | lvlvalue.Name = "Strength" |
19 | local convalue = Instance.new( "IntValue" ,leaderstats) |
20 | lvlvalue.Name = "Constitution" |
22 | local agivalue = Instance.new( "IntValue" ,leaderstats) |
23 | lvlvalue.Name = "Agility" |
25 | local intelvalue = Instance.new( "IntValue" ,leaderstats) |
26 | lvlvalue.Name = "Intelligence" |
30 | local GetSaved = DSService:GetAsync(uniquekey) |
32 | colvalue.Value = GetSaved [ 1 ] |
33 | lvlvalue.Value = GetSaved [ 2 ] |
34 | strvalue.Value = GetSaved [ 3 ] |
35 | convalue.Value = GetSaved [ 4 ] |
36 | agivalue.Value = GetSaved [ 5 ] |
37 | intelvalue.Value = GetSaved [ 6 ] |
39 | local assigndata = { colvalue.Value,lvlvalue.Value,strvalue.Value,convalue.Value,agivalue.Value,intelvalue.Value } |
40 | DSService:SetAsync(uniquekey,assigndata) |
44 | game.Players.PlayerRemoving:connect( function (plr) |
45 | local uniquekey = 'id-' ..plr.userId |
46 | local coltable = { plr.leaderstats.Col.Value } |
47 | local lvltable = { plr.leaderstats.lvl.Value } |
48 | local strtable = { plr.leaderstats.str.Value } |
49 | local contable = { plr.leaderstats.con.Value } |
50 | local agitable = { plr.leaderstats.agi.Value } |
51 | local inttable = { plr.leaderstats.int.Value } |
52 | local savedata = { coltable,lvltable,strtable,contable,agitable,inttable } |
53 | DSService:SetAsync(uniquekey,savedata) |
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:
1 | local Attributes = Player.attributes:GetChildren() |
2 | for i = 1 , #Attributes do |
3 | Player:SaveNumber(Attributes [ i ] .Name, Attributes [ i ] .Value) |
Would that be feasible? Less optimized/more optimized/roughly the same?