All you have to do is use the same thing you used for coins
and gems
. You need to use GetAsync
in order to get the values, and check if they've been set.
As the wiki states, "If the key does not exist, [GetAsync] returns nil." Using this information, you can determine whether or not they've played the game before, and set it to either the saved value, or "Default".
01 | local DS 1 = game:GetService( "DataStoreService" ):GetDataStore( "Coins" ) |
02 | local DS 2 = game:GetService( "DataStoreService" ):GetDataStore( "Gems" ) |
03 | local DS 3 = game:GetService( "DataStoreService" ):GetDataStore( "equippedKnife" ) |
04 | local DS 4 = game:GetService( "DataStoreService" ):GetDataStore( "equippedGun" ) |
06 | game.Players.PlayerAdded:connect( function (player) |
07 | local id = player.UserId; |
09 | local stats = Instance.new( "IntValue" , player) |
10 | stats.Name = "leaderstats" |
12 | local coins = Instance.new( "IntValue" , stats) |
14 | coins.Value = DS 1 :GetAsync(id.. "_DS1" ) |
16 | local gems = Instance.new( "IntValue" , stats) |
18 | gems.Value = DS 2 :GetAsync(id.. "_DS2" ) |
20 | local inGame = Instance.new( "BoolValue" , player) |
21 | inGame.Name = "InGame" |
23 | local equippedKnife = Instance.new ( "StringValue" , player) |
24 | equippedKnife.Name = "equippedKnife" |
25 | equippedKnife.Value = DS 3 :GetAsync(id.. "_DS3" ) or "Default" |
27 | local equippedGun = Instance.new( "StringValue" , player) |
28 | equippedGun.Name = "equippedGun" |
29 | equippedGun.Value = DS 4 :GetAsync(id.. "_DS4" ) or "Default" |
32 | DS 1 :SetAsync(player.UserId.. "_DS1" , coins.Value) |
35 | DS 2 :SetAsync(player.UserId.. "_DS2" , gems.Value) |
38 | DS 3 :SetAsync(player.UserId.. "_DS3" , equippedKnife.Value) |
39 | print ( "Saved knifeEquipped" ) |
41 | DS 4 :SetAsync(player.UserId.. "_DS4" , equippedGun.Value) |
42 | print ( "Saved gunEquipped" ) |
You may want to look into using a table to save values as that may be a bit neater, but this way would work fine as well.
Hope this helped.