Need help with module script

Hello guys,
I am trying to create a module function that will allow to give tags to players but I am stuck.

function module:giveTag(tagName : tag, player : Instance)
	local Table = {
		Positions = {
			["Top"] = Vector3.new(0,2.5,0),
			["Center"] = Vector3.new(0, 1.85, 0),
			["Buttom"] = Vector3.new(0, 1.35, 0),
		},
		Colors = {
			["Light_blue"] = Color3.new(0, 255, 225),
			["Dark_blue"] = Color3.new(0, 0.0156863, 1),
		},
		Warnings = {
			["No tag"] = "TAG: NO TAG FOUND!",
			["No tag color"] = "TAG: NO COLORS WERE INDICATED!",
			["No tag position"] = "TAG: NO POSITION WERE INDICATED!",
			
			["No player found"] = "TAG: NO PLAYER FOUND!",
		}
	}
	
	-- // SERVICES \\ --
	local getReplicatedStorage = game:GetService("ReplicatedStorage")

	-- // VARIABLES \\ --
	local tag = nil
	local playerFound = nil
	-- // PCALLS \\ --
	local isTagFound = pcall(function()
		tag = getReplicatedStorage.Tags:FindFirstChild(tagName)
	end)
	if not isTagFound then warn(Table.Warnings["No tag"]) return end
	local isPlayerFound = pcall(function()
		playerFound = workspace:FindFirstChild(player.Name):FindFirstChild("Character")
	end)
	if not isPlayerFound then warn(Table.Warnings["No player found"]) return end
	local clone = tag:Clone()
	print(clone.Parent) -- If I change this to print just clone, it returns the Instance but if I change it to clone.Parent it will be nil. I tried to parent this to the playerFound.Head but the output shows 0:14:56.351  ServerScriptService.ModuleScripts.playerFunctions:143: attempt to index nil with 'Head' 
end
1 Like

Using :FindFirstChild("Character") will cause playerFound to be nil since workspace:FindFirstChild(player.Name) already returns the player’s character.

Regardless, the way you’re going about this is not ideal. If you already have a Player object, you can get the character using the Player:GetPlayerFromCharacter() method.

Furthermore, to give a tag to the character, you should be using CollectionServices AddTag function.

1 Like

Hi. Thank you for the suggestion.

This works but I will look into your suggestion maybe it is a better way to do it

	local isPlayerFound = pcall(function()
		playerFound = workspace[tostring(player)]
	end)
	if not isPlayerFound then warn(Table.Warnings["No player found"]) return end
	local clone = tag:Clone()
	clone.Parent = playerFound.Head

Hey, if you’re not experiencing further issues with this topic, please mark my original response as the solution to help anyone else experiencing a similar issue :slight_smile:

I have just one more question.

Why when I do
getReplicatedStorage.Tags:FindFirstChild(tagName) (it will return true no matter what the tagName is?)

When I do
getReplicatedStorage.Tags[tagName] (it will return true as long as the tag Instance is found)

getReplicatedStorage.Tags:FindFirstChild(tagName) does not return True/False. It returns either the object you are looking for or nil if that object does not exist. In this case, if the tag you are searching for does not exist, it will return nil. This can be seen in the FindFirstChild Documentation.

getReplicatedStorage.Tags[tagName] will raise an error if the tag is not a child of getReplicatedStorage.Tags, if it is, it will return the tag object.

As stated in the original response, you should explore CollectionService over the method you are using now.

I hope this answers your question.

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