Why can't my scripts get information from my module script?

My script is not able to reference a value from a module script.
For reference, Ranks is a module script located in ServerScriptService.

It contains a table full of ranks, but all that you need to see is this:

local Ranks = {}

Ranks.RankValues = {
  ["Baron"] = {12, 2500, Color3.fromRGB(255,255,0)}
}

return Ranks

Another script in ServerScriptService runs when the player is added:

local Players = game:GetService("Players")
local Ranks = require(game:GetService("ServerScriptService"):WaitForChild("Ranks"))

Players.PlayerAdded:Connect(function(plr)
     plr.CharacterAdded:Connect(function(Character)
          local Rank = plr:WaitForChild("Rank") --This exists already from another script
          if Ranks.RankValues[plr.Rank.Value][1] <= 4 then --Assume Rank = "Baron"
              --Do things
          end
     end)
end)

The output errors that I am attempting to index nil with number on the line where it says
if Ranks.RankValues[plr.Rank.Value][1] <= 4 then.
This error occurs while my Rank value equals “Baron”. It’s a string value just like in the table. The error happens in multiple different scripts while trying to perform similar checks.

According to the module script, Ranks.RankValues[“Baron”][1] SHOULD equal 12, though, not nil? Any ideas as to why this doesn’t get checked properly?

Add some warns to debug. I copied your code and added warns and the creation of the StringValue and it works as intended:

local Players = game:GetService("Players")
local Ranks = require(game:GetService("ServerScriptService"):WaitForChild("Ranks"))

Players.PlayerAdded:Connect(function(plr)
	-- FOR DEBUG
	local newVal = Instance.new("StringValue")
	newVal.Name = "Rank"
	newVal.Value = "Baron"
	newVal.Parent = plr
	
	plr.CharacterAdded:Connect(function(Character)
		local Rank = plr:WaitForChild("Rank") --This exists already from another script
		warn("Current Rank Value in player:", Rank.Value)
		if Ranks.RankValues[plr.Rank.Value][1] <= 4 then --Assume Rank = "Baron"
			--Do things
			warn("value in table:", Ranks.RankValues[plr.Rank.Value][1])
		end
	end)
end)

Remove plr. to use the variable.

This script should work fine. You probably made a typo in the other script.

Did some debugging and the issue was a lot crazier than I thought but this helped me get on track to solve it. Thank you so much

1 Like

It wasn’t a typo in the other script, it ended up being some weird long-wound issue with the way I was getting the table, but I did figure it out. Thanks for the help anyways!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.