Issues with script that requires a module

The title isn’t very clear, but basically, I have a script that requires lines of a module script based on the player’s leaderstat value. Problem is, I’m unsure if the script is for sure working as the print statements I’m using are not printing anything.

Script:

local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent)

local level = tostring(player:WaitForChild("leaderstats").level.Value)
local XP = tostring(player:WaitForChild("leaderstats").XP.Value)

local Module = require(game.ReplicatedStorage.Modules.XPRequiredLevels)
local nextlevel = nil

script.Parent.Nickname.Rank.Text = script.Parent.Parent.Parent.Name
script.Parent.Level.Text = 'Level '..XP..''
local humanoid script.Parent.Parent.Parent:FindFirstChild("Humanoid")

humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None


for i, v in pairs(Module) do
	if i == level then
		playerlevel = level
		local nextlevel = playerlevel + 1
	end
end

print(playerlevel)
print(nextlevel)

Here’s the model:
Nametag2.rbxm (10.3 KB)

Inside your for loop, you declared “nextlevel” again. When you leave the scope of the loop the value is lost, so it never prints. Remove the “local” declaration in the for loop. Also, playerlevel is not declared or initialized to anything, so its in the global scope. Also, you are missing the “=” for the humanoid initialization.
That should fix it.

local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent)

local level = tostring(player:WaitForChild("leaderstats").level.Value)

local XP = tostring(player:WaitForChild("leaderstats").XP.Value)

local Module = require(game.ReplicatedStorage.Modules.XPRequiredLevels)

local nextlevel = nil
local playerlevel = 0

script.Parent.Nickname.Rank.Text = script.Parent.Parent.Parent.Name
script.Parent.Level.Text = 'Level '..XP..''
local humanoid = script.Parent.Parent.Parent:FindFirstChild("Humanoid")

humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None


for i, v in pairs(Module) do
	if i == level then
		playerlevel = level
	    nextlevel = playerlevel + 1
	end
end

print(playerlevel)
print(nextlevel)

That allowed the script to print now, although, it seems that now the loop isn’t running as A. what’s returned isn’t the actual data (level returns as zero when the leaderstat is one, nextlevel is nill), and putting the prints inside the loop doesn’t do anything at all.

If you want me to debug it, you need to include all the files required by your script. I explained why the print lines weren’t working, I even tested it with some faked data. So, that problem is solved.

Modules.rbxm (839 Bytes)
Modules folder goes in ReplicatedStorage, the script included with my original post goes in ServerSciptService.

Let me know if you have solved the issue.

You’re setting level equal to a string and then comparing it to the numerical indexes in your module. Because “1” is never equal to 1, your loop will never run past your if statement.

Either one of these steps will suffice in making your code run as expected :

  1. Remove your tostring method which is wrapped around your level and XP declaration statements - local level = player:WaitForChild("leaderstats").level.Value

  2. Change your XPRequiredLevels module to be a key-value table, i.e. change 1 to "1".

This did work, however after modifying my script a bit to make sure that whenever the player gains xp, the level is checked to see if they have enough XP to rank up is not working. For example, I have my leaderstat XP level set to 100 which means my level should be 3 with nextlevel being 4, but it still is only saying my level is one with nextlevel being 2.

local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent)

local level = player:WaitForChild("leaderstats").level.Value

local XP = player:WaitForChild("leaderstats").XP.Value

local Module = require(game.ReplicatedStorage.Modules.XPRequiredLevels)

local nextlevel = nil
local playerlevel = 0

script.Parent.Nickname.Rank.Text = script.Parent.Parent.Parent.Name
script.Parent.Level.Text = 'Level '..XP..''
local humanoid = script.Parent.Parent.Parent:FindFirstChild("Humanoid")

humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None


for i, v in pairs(Module) do
	if i == level then
		playerlevel = level
		nextlevel = playerlevel + 1
	end
	if level > nextlevel then
		playerlevel = nextlevel
	end
end

print(playerlevel)
print(nextlevel)

You need to actually check the xp required in your ModuleScript. As it stands, your code is only checking the player’s level, which is always being set to 1.

So how would I go about sending that data from the module to the script within the player’s BillboardGUI?

for i,v in pairs returns the index of the Module as i, as the value of Module at index i as v. Compare XP to v, and you can find the correct level.

for i, v in pairs(Module) do
	local level = 1
	
	if v <= XP then
		level = i
		
		
	else
		playerlevel = level
		nextlevel = level + 1
		
		break
	end
end

Still having the same issue as before even with this change, level is still one and nextlevel is equal to 2. Is it perhaps because level is being defined locally in the loop and the actual leaderstat itself is not being changed?

I really do not understand what you are trying to do here. Can you explain at a higher level. The
code doesn’t make much sense to me, but I have attempted to fix some of the bugs.
Also there is no leaderstats module included, which is helpful for debugging.
What are you trying to accomplish?
Are you determining level from XP? or XP from level?
What is playerlevel compared to level?
And the names of your text boxes are confusing.
However, I have included a debugged version of what you provided.
The biggest change is disabling the script in starterscripts and only
enabling the cloned version.
NameTagBugs.rbxl (46.7 KB)