How do I improve this level bar code?

So i have a code which basically makes the ui change for only certain levels, here:

  • Below is the image of example level 1-10 and then level 11
    Immagine 2024-08-28 123643
    Immagine 2024-08-28 124011

So as you see, it works pretty well but unfortunately there is a problem, when i try to add another level, [example:12] it doesn’t work.

I will attack the code below.


> local player = game.Players.LocalPlayer
> local Exp = player:FindFirstChild("Data"):WaitForChild("Exp")
> local Levels = player:FindFirstChild("Data"):WaitForChild("Levels")
> local VS = require(game:GetService("ReplicatedStorage").Game.Modules:WaitForChild("EasyVisuals"))
> 
> Exp.Changed:Connect(function()
> 	script.Parent.Text = ""..Exp.Value.."/".. tostring(100 * (Levels.Value + 1))
> end)
> 
> Levels.Changed:Connect(function()
> 	if Levels.Value == 11 then
> 		script.Parent.Text = "Max Level"
> 		VS.new(script.Parent, "IceStroke", 1, 1)
> 		VS.new(script.Parent.Parent.Decore, "IceStroke", 1, 1)
> 		VS.new(script.Parent.Parent.Parent.Background, "IceStroke", 1, 1)
> 		VS.new(script.Parent.Parent.Level, "IceStroke", 1, 1)
> 		--script.Parent.Parent.Level.UIGradient.Enabled = true
> 	else
> 		script.Parent.Text = ""..Exp.Value.."/".. tostring(100 * (Levels.Value + 1))
> 		VS.new(script.Parent.Parent.Parent.Parent.LevelUp.TextLabel, "Rainbow", 1, 1)
> 		VS.new(script.Parent.Parent.Parent.Parent.LevelUp.ImageLabel, "Rainbow", 1, 1)
> 	end
> end)
> 
> if Levels.Value == 11 then
> 	script.Parent.Text = "Max Level"
> 	VS.new(script.Parent, "IceStroke", 1, 1)
> 	VS.new(script.Parent.Parent.Decore, "IceStroke", 1, 1)
> 	VS.new(script.Parent.Parent.Parent.Background, "IceStroke", 1, 1)
> 	VS.new(script.Parent.Parent.Level, "IceStroke", 1, 1)
> 	--script.Parent.Parent.Level.UIGradient.Enabled = true
> else
> 	script.Parent.Text = ""..Exp.Value.."/".. tostring(100 * (Levels.Value + 1))
> 	VS.new(script.Parent.Parent.Parent.Parent.LevelUp.TextLabel, "Rainbow", 1, 1)
> 	VS.new(script.Parent.Parent.Parent.Parent.LevelUp.ImageLabel, "Rainbow", 1, 1)
> end

Thank you so much.

2 Likes

You’ve only used if for 11. shouldn’t you just add another block for another level like you did for eleven?

Example

if Levels.Value == 11 then
     -- rest of your code
elseif Levels.Value == 12 then
     -- ...
else
     -- rest of your code
end
1 Like

Hey, thank you for the response, but i looked into it more and found out its kind of a bug of the module script, thank you for responding!

2 Likes

turn the entire block into a local function so you dont have a huge wall of copy paste

local function isMax()
> if Levels.Value == 11 then
> 	script.Parent.Text = "Max Level"
> 	VS.new(script.Parent, "IceStroke", 1, 1)
> 	VS.new(script.Parent.Parent.Decore, "IceStroke", 1, 1)
> 	VS.new(script.Parent.Parent.Parent.Background, "IceStroke", 1, 1)
> 	VS.new(script.Parent.Parent.Level, "IceStroke", 1, 1)
> 	--script.Parent.Parent.Level.UIGradient.Enabled = true
> else
> 	script.Parent.Text = ""..Exp.Value.."/".. tostring(100 * (Levels.Value + 1))
> 	VS.new(script.Parent.Parent.Parent.Parent.LevelUp.TextLabel, "Rainbow", 1, 1)
> 	VS.new(script.Parent.Parent.Parent.Parent.LevelUp.ImageLabel, "Rainbow", 1, 1)
> end
> end
>
> isMax()
> Levels.Changed:Connect(function()
> 	isMax()
> end)

Thank you for suggesting me this! Ill try to implement this code!