Nontraditional Leveling Bar

Hello everyone, so I’ve recently been improving my scripting skills and trying to write efficient code due to previous written scripts being written poorly and causing issues later on when I need to adjust/add new content

Server Script:

local module = require(game.ServerScriptService.ModuleScript)
local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")
local Request = RS:WaitForChild("RequestData")

local Default = {
	SpeedLevel = 1,	
	SpeedExp = 0,
	Level = 1,
	Exp = 0, 
}

function Add(Player)
	module.AddPlayer(Player, Default)
	end

function DataRequest(Player)
	local Data = module.ReturnData(Player)
	Request:FireClient(Player, Data)
end

function PartExp(Touch) -- This is just a simple function for testing will be removed later
	if Touch.Parent:FindFirstChild("Humanoid") then
	local Player = Players[Touch.Parent.Name]
	module.AddExp(Player, 400)
	print("Added")
	end
end

game.Workspace.Part.Touched:Connect(PartExp)
Request.OnServerEvent:connect(DataRequest)
Players.PlayerAdded:Connect(Add)

Module Script:

local module = {}
local Player_Stats = {}

local RS = game:GetService("ReplicatedStorage")
local Request = RS:WaitForChild("RequestData")

function module.AddPlayer(Player, Stats)
	Player_Stats[Player] = Stats
end

function module.RemovePlayer(Player)
	Player_Stats[Player] = nil
end

function module.ChangeStat(Player, Stat, Value)
	Player_Stats[Player][Stat] = Value
end

function module.ReturnData(Player)
	return Player_Stats[Player]
end

function module.AddExp(Player, Value) -- Level up System
	local Stats = Player_Stats[Player]
	Stats["Exp"] = Stats["Exp"] + Value
	
	while Stats["Exp"] > Stats["Level"] * 125  do
		
		Stats["Exp"] = Stats["Exp"] -  Stats["Level"] * 125
		Stats["Level"] = Stats["Level"] + 1

	end
	Request:FireClient(Player, module.ReturnData(Player)) -- The Data is updated so we send the Client new Data
end

return module

Local Script:


local Player = game.Players.LocalPlayer
local PlayerGui = Player.PlayerGui

local TweenService = game:GetService("TweenService")
local TInfo = TweenInfo.new(.01)
local TInfo2 = TweenInfo.new(.2)

local RS = game:GetService("ReplicatedStorage")
local Request = RS:WaitForChild("RequestData")

local Level = 0
local Data = nil

local HUD = PlayerGui:WaitForChild("HUD").Bar_BackGround
local LevelGui = HUD.Level.Bars
local Info = HUD.Info
local LevelInfo = Info.LevelInfo

local BarRunning = false


function LevelToBar()
	BarRunning = true
	while Level < Data["Level"] do -- While Level Counter is less than your current Level
		Level = Level + 1
		for i = 1, 14 do -- Cycles through all of the Bars Frames and Tweens them to be visible
			local tween = TweenService:Create(LevelGui[i], TInfo, {BackgroundTransparency = 0})
			tween:Play()
			wait(.01)
		end
		LevelInfo.Text = "LEVEL   " .. Level
			for i = 1, 14 do -- Sets all of their transparency to be semi - invisible
			LevelGui[i].BackgroundTransparency = .6
			end
			
	end -- it repeats until your level counter is == to your current level
	
	if Level == Data["Level"] then -- If your Level Counter is == to your Current level
	
	local RoundedAmount = math.floor((14 / ((Data["Level"] * 125) / Data["Exp"])) + 0.5) -- Formula to get the Frames needed to be Filled based on your Exp to Needed Exp Ratio 
	
		for i = 1, 14 do -- Cycles through all Frames and Tweens the Frames that need to be Visible
			if i <= RoundedAmount then
			local tween = TweenService:Create(LevelGui[i], TInfo2, {BackgroundTransparency = 0})
			tween:Play()
			wait(.2)
			end
		end
		
	end

BarRunning = false

end


function barsetup() -- Runs once when you first join to setup the bars with your current data as tweening isnt necessary when you first join
	local RoundedAmount = math.floor((14 / ((Data["Level"] * 125) / Data["Exp"])) + 0.5) -- Formula to get the Frames needed to be Filled based on your Exp to Needed Exp Ratio 
	Level = Data["Level"]
	LevelInfo.Text = "LEVEL   " .. Data["Level"]
	
	for i = 1, 14 do
		if i <= RoundedAmount then
		LevelGui[i].BackgroundTransparency = 0
		else 
		LevelGui[i].BackgroundTransparency = .6
		end
	end
end


Request.OnClientEvent:connect(function(SentData) -- When the Players Data Updates on Server I fire the client and send the new Data
	if Data == nil then -- If the Data Variable is nil then set the data and run the barsetup function
		Data = SentData
		barsetup()
	elseif BarRunning == false then -- Updates Level Bar
		Data = SentData
		LevelToBar()
		
	end
	

end)

Request:FireServer() -- Asks for Data when you first join

Bar In Game:

Gui in Explorer:
image

I need tips on how I can improve what I’ve written, or if I have done anything incorrectly/poorly

I also have one issue, when I gain exp while the final tween is happening (the slow tween to finish the level) the bar wont update until after it ends and I gain more exp this is due to the BarRunning debounce I believe if anyone has a suggestion to fix it let me know

If you have any questions just reply

Thanks

2 Likes

Actually going to scrap the entire local side and redo it, as it’s not very efficient, if anyone has any ideas let me know