Ok, lets try it
Firs Step : We need to know how to acces to the DataStoreService and how it works! So lets see how we can get the service!
1 | local ds = game:GetService( "DataStoreService" ) |
That's how we get the DataStoreService in one simple line of code!
Second Step : Second, we need to know how to use correctly the DataStore so we have to make another variable that contains the data of the currency or etc we want to Store on the service!
1 | local ds = game:GetService( "DataStoreService" ) |
2 | local moneyDs = ds:GetDataStore( "moneyData" ) |
That's how we create a variable with the data we want to save. So lets make now a leaderstats functional with the SaveData
Third Step : Enjoy coding! You always have to enjoy what are you doing! Play some music while you script or even you can play a game before to be relaxed!
Lets get into code!
01 | local ds 1 = game:GetService( "DataStoreService" ) |
02 | local moneyDs = ds 1 :GetDataStore( "moneyData" ) |
03 | local ds 2 = game:GetService( "DataStoreService" ) |
04 | local xpDs = ds 2 :GetDataStore( "xpData" ) |
05 | local ds 3 = game:GetService( "DataStoreService" ) |
06 | local lvlDs = ds 3 :GetDataStore( "levelData" ) |
07 | local ds 4 = game:GetService( "DataStoreService" ) |
08 | local rxpDs = ds 4 :GetDataStore( "requiredxpData" ) |
10 | game.Players.PlayerAdded:Connect( function (plr) |
11 | local folder = Instance.new( "Folder" , plr) |
12 | folder.Name = "leaderstats" |
14 | local money = Instance.new( "IntValue" , folder) |
16 | money.Value = moneyDs:GetAsync(plr.UserId) or 0 |
18 | money.Changed:Connect( function () |
19 | moneyDs:SetAsync(plr.UserId, money.Value) |
23 | local xp = Instance.new( "IntValue" , folder) |
25 | xp.Value = xpDs:GetAsync(plr.UserId) or 0 |
26 | xp.Changed:Connect( function () |
27 | xpDs:SetAsync(plr.UserId, xp.Vaue) |
30 | local rxp = Instance,new( "IntValue" , folder) |
31 | rxp.Name = "RequiredXP |
32 | rxp.Value = rxpDs:GetAsync(plr.UserId) or 100 |
33 | rxp.Changed:Connect( function () |
34 | rxpDs:SetAsync(plr.UserId, rxp.Value) |
37 | local level = Instance.new( "IntValue" , folder) |
39 | level.Value = lvlDs:GetAsync(plr.UserId) or 0 |
40 | level.Changed:Connect( function () |
41 | lvlDs:SetAsync(plr.UserId, level.Value) |
44 | if xp.Value > = rxp.Value then |
45 | xp.Value = xp.Value - rxp.Value |
47 | level.Value = level.Value + 1 |
48 | elseif xp.Value < = rxp.Value then |
49 | print ( "You don't have enough experience! Get more to level up!" ) |
53 | game.Players.PlayerRemoving:Connect( function (plr) |
54 | moneyDs:SetAsync(plr.UserId, plr.leaderstats.Money.Value) |
55 | xpDs:SetAsync(plr.UserId, plr.leaderstats.XP.Value) |
56 | lvlDs:SetAsync(plr.UserId, plr.leaderstats.Level.Value) |
60 | local warnMessage = "The code is incorrect" |
That's how we use the DataStore correctly and easy. I write all the code here on scriptinghelpers so that's the why I don't answer faster.
I hope my answer already solve your question! If its like that please accept my answer. That will help me a lot!
Keep scripting!