Attempt to index nil with 'ActiveRanks' with self

Basically i’m trying to make a custom admin module for somewone with a custom admin system. However i encountered an issue whilr making the module. As the tittle name. it break and bypass the pcall protection too. How can i potentially fix this issue?

local MainModule = {}

MainModule.ActiveRanks = {}

-- Services "dont touch"
local ServerScriptService = game:GetService("ServerScriptService")
local DataStoreService = game:GetService("DataStoreService")
local TextChatService = game:GetService("TextChatService")
local Players = game:GetService("Players")

-- !How to set ranks
-- Return the owner of the game use "game.CreatorId"

-- Set the admin DataStore Name
local DatastoreName = "AdminRankStore"

-- Set ranks
local Ranks = {
	["Owner"] = {game.CreatorId},
	["Devs"] = {1453085560},
	["Head Admin"] = {},
	["Admin"] = {},
	["Moderator"] = {},
	["Invited"] = {},
}

-- Please assign the command to a requirment
-- Add Restricted if you don't want the command to be used for the same rank

local Commands = {
	["kick"] = {"Moderator","Restricted"},
	["ban"] = {"Moderator","Restricted"},
	["give"] = {"Head Admin"},
	["teleport"] = {"Moderator"},
	["fly"] = {"Devs"},
	["add"] = {"Devs"},
	["spawn"] = {"Devs"},
	["invisible"] = {"Moderator"},
	["kill"] = {"Devs"},
	["rank"] = {"Owner"},
	["unrank"] = {"Owner"},
	["saverank"] = {"Owner"},
	["unsaverank"] = {"Owner"},
	["gravity"] = {"Devs"},
	["announce"] = {"Admin"},
}

local success,errorm = pcall(function()
	-- Set parent back to where its secure
	script.Parent.Parent = ServerScriptService
	
	-- Check the eligibility
	if TextChatService.ChatVersion == Enum.ChatVersion.LegacyChatService then
		warn("LegacyChatService is not eligible, please change the ChatVersion to TextChatService in TextChatService ChatVersion to use Admin")
		return MainModule
	end
	
	-- Create command folder
	local CommandsFolder = Instance.new("Folder",TextChatService)
	CommandsFolder.Name = "AdminCommands"
	
	-- Create triggerable commands
	for i,v in Commands do
		local command = Instance.new("TextChatCommand",CommandsFolder)
		
		command.Name = i
		command.PrimaryAlias = "/"..i
		
		task.spawn(function()
			command.Triggered:Connect(function(originTextSource: TextSource, unfilteredText: string)  
				print("triggered")
			end)
		end)
	end
	
	-- Connect to players joigning
	Players.PlayerAdded:Connect(function(player)
		for i,v in Ranks do
			if table.find(v,player.UserId) then
				print(i,v)
				self.ActiveRanks[table.find(v,player.UserId)] = i
			end
		end
	end)
end)

if errorm then
	warn("Admin load fail: "..errorm)
end

return MainModule
1 Like
  1. The error bypasses the pcall because it’s occurring in an event handler, which has its code executed in a separate thread.

  2. self is clearly undefined. Did you mean MainModule?

yea self is = to MainModule normally by default because its a module script?

No. That is not how self works. self is an implicit parameter to functions associated with tables. Read this reply to a post for more details

On another note, your task.spawn call is redundant. As I said, event handlers are already executed in a separate thread

the task.spawn is for preventing each command to trigger each other by using separate threads.

I’m not sure what you mean, but I can guarantee you that your task.spawn call is not providing any functional value here

1 Like

Nah. It doesn’t work like that.

If you want to use self in this case, then you should replace all occurances of MainModule with self, so the script would start like

local self = {}
self.ActiveRanks = {}

...
1 Like

I found a way to mess around and use self to locate as module. But it could work too

local MainModule = {}
self = MainModule

MainModule.ActiveRanks = {}

This is ridiculous. Do not create redundant aliases. Use MainModule as is

1 Like