Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I relate level and XP?

Currently I'm working on a level system:

local levels = {
	{level = 1, XP = 500};
	{level = 2, XP = 1000};
	{level = 3, XP = 2000};
	{level = 4, XP = 3000};
	{level = 5, XP = 5000};
}

script.Parent.XP.Changed:Connect(function()
	for i = 1,#levels do
		
		if script.Parent.XP.Value > levels[i].XP then
			print(levels[i].level)
		end
	end
end)

The table contains the each level's XP, For an example, if your XP's Value is larger than 1000 but smaller than 2000, you will be in level 2, how do I perform that? I can't really figure a way on that.

5 comments

0

Do you mean like, when the player reaches level 2 a value updates or something?

0

I'll work on it, hold on.

0

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

0

i will try to fix it

2 answers

I might fixed your script

local levels = {
    ["level0"] = 0;
    ["level1"] = 500;
    ["level2"] = 1000;
    ["level3"] = 2000;
    ["level4"] = 3000;
    ["level5"] = 5000
}
local level = 0
script.Parent.XP.Changed:Connect(function(con)
        if con > levels["level"..level] then
            print(levels["level"..level])     
	end
end)

1 comment

1

try this



local players = game:GetService("Players")
local rs = game:GetService("ReplicatedStorage")

local loadedPlayers = {}
local dataStore = game:GetService("DataStoreService"):GetDataStore("Datastore")
local dataStoreWait = 150


-- CONFIGURATIONS
local containerName = "leaderstats" -- what the object containing the level and experience will be called
local levelName = "Level" -- what the level will be called
local experienceName = "XP" -- what the experience will be called

local saves = true -- if the stats will be saved in this script

local function equation(l)
		-- l is level
	--[[ Summation for small experience level-ups
		local sum = 0
		
		for i = 1,l do
			sum = sum + i
		end
	--]]
	
	return l * 100
end

-- FUNCTIONS

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) -- calculates the level
		-- l is level and x is experience
	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)
		-- p is player and d is data (the container)
	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)
		-- p is player and d is data (the container)
	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




-- PLAYER STUFF

local function playerAdded(p)
	local container,level,experience = setUp(p)
	local experienceUpdateDebounce = true -- woah
	
	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




1 comment

0

Did you just copy and paste from somewhere else?

This discussion is preserved as it was. New answers, comments, and votes are closed.