I’m trying to make a system where a player levels up(they are getting constant exp for testing purposes)
but whenever the exp that is needed updates, it goes back to 100. Its suppose to get the level and multiply it by 100, it does that but it just resets back to 100 after a second or two
Client:
local clipping = script.Parent:WaitForChild("BarClipping")
clipping.ClipsDescendants = true
local expTxt = script.Parent:WaitForChild("Experience")
local levelTxt = script.Parent:WaitForChild("Level")
local expVal = game.Players.LocalPlayer:WaitForChild("Stats"):WaitForChild("Experience")
local levelVal = game.Players.LocalPlayer.Stats:WaitForChild("Level")
local previousExp = expVal.Value
function updateGui()
local neededExp = math.floor(levelVal.Value * 100)
local previousLevelExp = math.floor((levelVal.Value - 1) *100)
local expDiff = neededExp - previousLevelExp
local currentExp = expVal.Value - previousLevelExp
local x = currentExp / expDiff
clipping.Size = UDim2.new(x, 0, 1, 0)
clipping.Bar.Size = UDim2.new(1 / x, 0, 1, 0)
expTxt.Text = currentExp .. "/" .. expDiff
levelTxt.Text = "Level " .. levelVal.Value
if expVal.Value >= neededExp then
local nextExpNeeded = math.floor((levelVal.Value + 1)* 100)
expTxt.Text = "0/" .. nextExpNeeded
levelTxt.Text = "Level " .. levelVal.Value + 1
clipping.Size = UDim2.new(0, 0, 1, 0)
end
previousExp = expVal.Value
end
updateGui()
expVal:GetPropertyChangedSignal("Value"):Connect(updateGui)
Server:
local dss = game:GetService("DataStoreService")
local levelDS = dss:GetDataStore("Levels")
function incrementExp(player, increment)
for i = player.Stats.Experience.Value, player.Stats.Experience.Value + increment do
player.Stats.Experience.Value = i
wait()
end
end
function saveData(player)
pcall(function()
local level = player.Stats.Level.Value
local exp = player.Stats.Experience.Value
levelDS:SetAsync(player.UserId .. "Level", {level, exp})
end)
end
game.Players.PlayerAdded:Connect(function(player)
local statsFolder = Instance.new("Folder", player)
statsFolder.Name = "Stats"
local levelVal = Instance.new("IntValue", statsFolder)
levelVal.Name = "Level"
levelVal.Value = 1
local expVal = Instance.new("IntValue", statsFolder)
expVal.Name = "Experience"
pcall(function()
local data = levelDS:GetAsync(player.UserId .. "-Level")
if data then
levelVal.Value = data[1]
expVal.Value = data[2]
end
end)
expVal:GetPropertyChangedSignal("Value"):Connect(function()
local neededExp = math.floor(levelVal.Value * 100)
if expVal.Value >= neededExp then
levelVal.Value += 1
end
end)
while wait(0.2) do
incrementExp(player, 20)
end
end)
game.Players.PlayerRemoving:Connect(saveData)
game:BindToClose(function()
for i, player in pairs(game.Players:GetPlayers()) do
saveData(player)
end
end)
video of it happening: