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...
game.Players.PlayerAdded:Connect(function(plr)
local folder = Instance.new("Folder", plr)
folder.Name = "leaderstats"
end
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!
local ds = game:GetService("DataStoreService")
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!
local ds = game:GetService("DataStoreService")
local moneyDs = ds:GetDataStore("moneyData")
game.Players.PlayerAdded:Connect(function(plr)
local folder = Instance.new("Folder", plr)
folder.Name = "leaderstats"
local currency = Instance.new("IntValue", folder)
currency.Name = "Money"
currency.Value = moneyDs:GetAsync(plr.UserId) or 0
currency.Changed:Connect(function()
moneyDs:SetAsync(plr.UserId, currency.Value)
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
moneyDs:SetAsync(plr.UserId, plr.leaderstats.Money)
end)
local warnMessage = "The code is incorrect!"
pcall(warnMessage)
That's how we make a leaderstats with DataStore and easily.
To make a Leveling System [Easy]...
local ds1 = game:GetService("DataStoreService")
local levelDs = ds1:GetDataStore("levelData")
local ds2 = game:GetService("DataStoreService")
local xpDs = ds2:GetDataStore("xpData")
local ds3 = game:GetService("DataStoreService")
local rxpDs = ds3:GetDataStore("rpxData")
game.Players.PlayerAdded:Connect(function(plr)
local folder = Instance.new("Folder", plr)
folder.Name = "leaderstats"
local level = Instance.new("IntValue", folder)
level.Name = "Level"
level.Value = levelDs:GetAsync(plr.UserId) or 1
level.Changed:Connect(function()
levelDs:SetAsync(plr.UserId, level.Value)
end)
local xp = Instance.new("IntValue", folder)
xp.Name = "XP"
xp.Value = xpDs:GetAsync(plr.UserId) or 0
xp.Changed:Connect(function()
xpDs:SetAsync(plr.UserId, xp.Value)
end)
local rxp = Instance.new("IntValue", folder)
rxp.Name = "RequiredXP"
rpx.Value = rxpDs:GetAsync(plr.UserId) or 1000
rpx.Changed:Connect(function()
rpx:SetAsync(plr.UserId, rpx.Value)
end)
if xp.Value >= rxp.Value then
xp.Value = xp.Value - rxp.Value
level.Value = level.Value + 1
rxp.Value = rxp.Value + 2000
elseif xp.Value <= rpx.Value then
print("You don't have enough experience! Get more!")
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
levelDs:SetAsync(plr.UserId, plr.leaderstats.Level.Value)
xpDs:SetAsync(plr.UserId, plr.leaderstats.XP.Value)
rxpDs:SetAsync(plr.UserId, plr.leaderstats.RequiredXP.Value)
end)
local warnMessage = "The code is incorrect!"
pcall(warnMessage)
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!
1 comment
I see no issue lol u should try removing what’s deprecated(idk what’s deprecated in the code lol)