Answered by
5 years ago Edited 5 years ago
Ok, lets try it!
First Step : First of all we need to know how to acces to the Player. And create values inside a folder named "leaderstats".
Example...
3 | game.Players.PlayerAdded:Connect( function (plr) |
4 | local folder = Instance.new( "Folder" , plr) |
5 | folder.Name = "leaderstats" |
We already added a folder, now we can add some values so we can use it like leaderstats. Can be IntValues or StringValues [For ranks].
Second Step : We can learn how to use a DataStore, but... How we acces to the DataStore
of a player?
Easy!
1 | local ds = game:GetService( "DataStoreService" ) |
2 | local moneyDs = ds:GetDataStore( "moneyData" ) |
See? Easy, right? We don't need more than 2 lines of code to acces to the DataStore
, but now... How we can start creating a correct leaderstats script?
Easy!
Third Step : Enjoy coding! You aways have to enjoy what are you doing! Play some music while you script or even play a game before to be relaxed.
Lets get into the code!
03 | local ds = game:GetService( "DataStoreService" ) |
04 | local moneyDs = ds:GetDataStore( "moneyData" ) |
09 | game.Players.PlayerAdded:Connect( function (plr) |
10 | local folder = Instance.new( "Folder" , plr) |
11 | folder.Name = "leaderstats" |
13 | local currency = Instance.new( "IntValue" , folder) |
14 | currency.Name = "Money" |
15 | currency.Value = moneyDs:GetAsync(plr.UserId) or 0 |
16 | currency.Changed:Connect( function () |
17 | moneyDs:SetAsync(plr.UserId, currency.Value) |
23 | game.Players.PlayerRemoving:Connect( function (plr) |
24 | moneyDs:SetAsync(plr.UserId, plr.leaderstats.Money) |
29 | local warnMessage = "The code is incorrect!" |
That's how we make a leaderstats with DataStore and easily.
To make a Leveling System [Easy]...
03 | local ds 1 = game:GetService( "DataStoreService" ) |
04 | local levelDs = ds 1 :GetDataStore( "levelData" ) |
05 | local ds 2 = game:GetService( "DataStoreService" ) |
06 | local xpDs = ds 2 :GetDataStore( "xpData" ) |
07 | local ds 3 = game:GetService( "DataStoreService" ) |
08 | local rxpDs = ds 3 :GetDataStore( "rpxData" ) |
13 | game.Players.PlayerAdded:Connect( function (plr) |
14 | local folder = Instance.new( "Folder" , plr) |
15 | folder.Name = "leaderstats" |
17 | local level = Instance.new( "IntValue" , folder) |
19 | level.Value = levelDs:GetAsync(plr.UserId) or 1 |
20 | level.Changed:Connect( function () |
21 | levelDs:SetAsync(plr.UserId, level.Value) |
24 | local xp = Instance.new( "IntValue" , folder) |
26 | xp.Value = xpDs:GetAsync(plr.UserId) or 0 |
27 | xp.Changed:Connect( function () |
28 | xpDs:SetAsync(plr.UserId, xp.Value) |
31 | local rxp = Instance.new( "IntValue" , folder) |
32 | rxp.Name = "RequiredXP" |
33 | rpx.Value = rxpDs:GetAsync(plr.UserId) or 1000 |
34 | rpx.Changed:Connect( function () |
35 | rpx:SetAsync(plr.UserId, rpx.Value) |
38 | if xp.Value > = rxp.Value then |
39 | xp.Value = xp.Value - rxp.Value |
40 | level.Value = level.Value + 1 |
41 | rxp.Value = rxp.Value + 2000 |
42 | elseif xp.Value < = rpx.Value then |
43 | print ( "You don't have enough experience! Get more!" ) |
49 | game.Players.PlayerRemoving:Connect( function (plr) |
50 | levelDs:SetAsync(plr.UserId, plr.leaderstats.Level.Value) |
51 | xpDs:SetAsync(plr.UserId, plr.leaderstats.XP.Value) |
52 | rxpDs:SetAsync(plr.UserId, plr.leaderstats.RequiredXP.Value) |
57 | local warnMessage = "The code is incorrect!" |
That's all you need to know.
I hope my answer already solve your question. If it did, please accept my answer. That will help me a lot!
Keep scripting!