local players = game:GetService("Players")
local rs = game:GetService("ReplicatedStorage")
local loadedPlayers = {}
local dataStore = game:GetService("DataStoreService"):GetDataStore("Datastore")
local dataStoreWait = 150
local containerName = "leaderstats"
local levelName = "Level"
local experienceName = "XP"
local saves = true
local function equation(l)
return l * 100
end
local function setUp(o)
local c,l,x = o:WaitForChild(containerName,.1)
if not c then
c = Instance.new("Folder")
c.Name = containerName
c.Parent = o
end
l,x = c:WaitForChild(levelName,.1),c:WaitForChild(experienceName,.1)
if not l then
l = Instance.new("IntValue")
l.Value = 1
l.Name = levelName
l.Parent = c
end
if not x then
x = Instance.new("NumberValue")
x.Name = experienceName
x.Parent = c
end
return c,l,x
end
local function getLevel(l,x)
local endLevel = l
local endExperience = x
if x >= equation(l) then
endLevel,endExperience = getLevel(l + 1, x - equation(l))
end
return endLevel,endExperience
end
local function attempt(f,t)
local s,x
t = t or 3
for i = 1,t do
s,x = pcall(f)
if s then
return x
end
end
warn(x)
end
local function saveData(p,d)
local data = attempt(function() return dataStore:GetAsync(p.UserId) end)
if data then
attempt(function() dataStore:SetAsync(p.UserId,{L = d:WaitForChild(levelName).Value;X = d:WaitForChild(experienceName).Value}) end)
else
attempt(function() return dataStore:SetAsync(p.UserId, {L = d:WaitForChild(levelName).Value;X = d:WaitForChild(experienceName).Value} ) end)
end
end
local function loadData(p,d)
local data = attempt(function() return dataStore:GetAsync(p.UserId) end)
if data then
d:WaitForChild(levelName).Value = data.L
d:WaitForChild(experienceName).Value = data.X
else
attempt(function() return dataStore:SetAsync(p.UserId, {L = d:WaitForChild(levelName).Value;X = d:WaitForChild(experienceName).Value} ) end)
end
end
local function calculateWait()
local pCount = #players:GetPlayers()
return math.max(15,300/(60 + pCount * 10) )
end
local function playerAdded(p)
local container,level,experience = setUp(p)
local experienceUpdateDebounce = true
if saves then
loadData(p,container)
loadedPlayers[p] = container
end
experience.Changed:Connect(function()
if experienceUpdateDebounce then
experienceUpdateDebounce = false
level.Value,experience.Value = getLevel(level.Value,experience.Value)
wait(.1)
experienceUpdateDebounce = true
end
end)
dataStoreWait = calculateWait()
end
for i,v in pairs(players:GetChildren()) do
spawn(function() playerAdded(v) end)
end
game.Players.PlayerAdded:Connect(playerAdded)
game.Players.PlayerRemoving:Connect(function(p)
dataStoreWait = calculateWait()
if loadedPlayers[p] then
saveData(p,p:WaitForChild(containerName))
loadedPlayers[p] = nil
end
end)
if saves then
local lastTick = tick()
while true do
wait(1)
local ticked = tick() - lastTick
if ticked >= dataStoreWait then
lastTick = tick()
for i,v in pairs(loadedPlayers) do
saveData(i,v)
end
end
end
end
if game:GetService("RunService"):IsStudio() then game:BindToClose(function() wait(3) end) end
5 comments
Do you mean like, when the player reaches level 2 a value updates or something?
I'll work on it, hold on.
Oh I get it, everytime the players level up. Their Experience gradually adds up so the player will need to full up that EXP until the next level
https://devforum.roblox.com/t/level-systems-part-1/296963
i will try to fix it