Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How do I relate level and XP?

Asked by 4 years ago

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.

0
Do you mean like, when the player reaches level 2 a value updates or something? BashGuy10 384 — 4y
0
I'll work on it, hold on. Mexual 2 — 4y
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 cherrythetree 130 — 4y
0
i will try to fix it gloveshun 119 — 4y

2 answers

Log in to vote
1
Answered by
gloveshun 119
4 years ago

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
try this gloveshun 119 — 4y
Ad
Log in to vote
0
Answered by 4 years ago


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
0
Did you just copy and paste from somewhere else? Headstackk 45 — 4y

Answer this question