I have 2 scripts, one for storing the data and one for the actual level system itself. (both in server script storage) I would like to have both in the same script if possible, and make it so that the level cap is 100.
Data store code /
local DataSaveService = game:GetService("DataStoreService")
local stat = DataSaveService:GetDataStore("Stats")
game.Players.PlayerAdded:Connect(function(plr)
local status = Instance.new("Folder", plr)
status.Name = "leaderstats"
local exp = Instance.new("IntValue", status)
exp.Name = "Exp"
local level = Instance.new("NumberValue", status)
level.Name = "Level"
local exps
local levels
local success, errormsg = pcall(function()
exps = stat:GetAsync(plr.UserId.."-Exp")
levels = stat:GetAsync(plr.UserId.."-Level")
end)
if success then
exp.Value = exps
level.Value = levels
print("Data saved")
else
print("Data didn't load correctly")
warn(errormsg)
end
local expreq = (105 * (level.Value + 1))
spawn(function()
while wait() do
if exp.Value >= expreq then
exp.Value = exp.Value - expreq
level.Value = level.Value + 1
end
end
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
local success, errormsg = pcall(function()
stat:SetAsync(plr.UserId.."-Level", plr.leaderstats.Level.Value)
stat:SetAsync(plr.UserId.."-Exp", plr.leaderstats.Exp.Value)
end)
if success then
print("Player data got saved")
else
print("Failed at saving data")
warn(errormsg)
end
end)
Level system code /
game.Players.PlayerAdded:Connect(function(player)
local level = Instance.new("IntValue", player)
level.Name = "Level"
level.Value = 1
local exp = Instance.new("IntValue", level)
exp.Name = "Current"
exp.Value = 0
local maxExp = Instance.new("IntValue", level)
maxExp.Name = "Max"
maxExp.Value = 100
exp.Changed:Connect(function(val)
if exp.Value >= maxExp.Value then
level.Value = level.Value + 1
exp.Value = 0
maxExp.Value = maxExp.Value * 1.05
end
end)
end)
workspace:WaitForChild("EXPcoin").ClickDetector.MouseClick:Connect(function(player)
player.Level.Current.Value = player.Level.Current.Value + 500
end)
