Need guidance on Module script

Hello guys,
I have this code that give players a tag by calling the tagName as a paremeter but I was unable to successful execute it. It comes back as nil

My eventManager

-- // SERVICES \\ --
local getReplicatedStorageService = game:GetService("ReplicatedStorage")
local getServerScriptService = game:GetService("ServerScriptService")

-- // ACCESS MODULE SCRIPT \\ --
local moduleScript = require(getServerScriptService.ModuleScripts:WaitForChild("playerFunctions"))

-- // REFERENCE \\ --
local eventFolder = getReplicatedStorageService:WaitForChild("Events")
-- // EVENT \\ --
local afkEvent = eventFolder.afk

afkEvent.OnServerEvent:Connect(function(player, button)
	if table.find(moduleScript.players.playersInField, player) then warn("Can't be afk while in game") return end
	
	if table.find(moduleScript.players.generalPlayers, player) then
		
		moduleScript:RemovePlayerFromTable(moduleScript.players.generalPlayers, player)
		moduleScript:AddPlayerToTable(moduleScript.players.afkPlayers, player)
		
		moduleScript:giveTag("Afk", player)
		
		button.Text = "Afk enabled"
	else
		moduleScript:AddPlayerToTable(moduleScript.players.generalPlayers, player)
		moduleScript:RemovePlayerFromTable(moduleScript.players.afkPlayers, player)
		
		moduleScript.removeTag("Afk", player)
		
		button.Text = "Afk disabled"
	end
end)

My module script

function module:giveTag(tagName: tag, player: player)
	local waitForCharacter = player.Character or player.CharacterAdded:Wait()
	
	waitForCharacter.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
	
	local tag = getReplicatedStorage:WaitForChild("Tags")[tagName]:Clone()
	if tagName == "Player" then
		tag.TextLabel.Text = player.Name
	end
	tag.Parent = player.Character:FindFirstChild("Head")
end
function module:removeTag(tagName: tag, player: player)
	if player.Character:FindFirstChild("Head")[tagName] then
		player.Character:FindFirstChild("Head")[tagName]:Destroy()
	end
end

Error

  01:47:33.408  ServerScriptService.ModuleScripts.playerFunctions:116: attempt to index nil with 'Character'  -  Server - playerFunctions:116
2 Likes

Which is the specific line that errors within the module script?

But this also indicates that player is nil, as the variable that you’re trying to reference stems from the parent, player. Make sure that you’re passing an actual player instance and not nil.

Hi,
This is the line that causes the error to appear. I can call the giveTag function but it errors out on removing tag.

moduleScript.removeTag(“Afk”, player)

1 Like

use : instead of .

moduleScript:removeTag("Afk", player)
Explanation

using : means that the table (moduleScript) is passed as the first argument. As you define the function with : it expects the first argument to be the table itself.

function module:removeTag(tagName: tag, player: player)

is the same as

function module.removeTag(self, tagName: tag, player: player)
1 Like

Thank you for the help :smiley:. I have fixed the problem with your guidance but sometimes when I call giveTag, it can return the character to be nil so I added this.

Sometimes when I call this function on a Roblox’s built-in event like playerAdded, the giveTag character will return nil. I have tried to counter this by using player.CharacterAdded but it still returns the character to be nil.

This is my final solution to solve the recent problem that I face

function module:giveTag(tagName : tag, player : player, delayAmount : wait)
	task.wait(delayAmount)
	player.Character.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None

	local tag = getReplicatedStorage:WaitForChild("Tags")[tagName]:Clone()
	if tagName == "Player" then
		tag.TextLabel.Text = player.Name
	end
	tag.Parent = player.Character:FindFirstChild("Head")
end

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