Attempt to index nil with 'value' Modual Script

Hey!

Im making this scrambler script but I want it to be able to be toggled so it checks if a value is true or false but its making an error when i do that.

Script:

local functionId = "editText"

local function doFilter(speaker, messageObject, channelName)
	local random = Random.new()
	local letters = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}

	--function getRandomString(length, includeCapitals)
	--	local length = length or 10
	--	local str = ''
	--	for i=1,length do
	--		local randomLetter = letters[random:NextInteger(1,#letters)]
	--		if includeCapitals and random:NextNumber() > .5 then
	--			randomLetter = string.upper(randomLetter)
	--		end
	--		str = str .. randomLetter
	--	end
	--	return str
	--end

	local DTValue = speaker.Backpack.DTValue -- error.
	if DTValue.Value == true then
		print("Player and DT True")
		local length = string.len(messageObject.Message)
		local str = ''
		for i=1,length do
			local randomLetter = letters[random:NextInteger(1,#letters)]
			if random:NextNumber() > .5 then
				randomLetter = string.upper(randomLetter)
			end
			str = str .. randomLetter
		end
		messageObject.Message = str
	end
end

local function runChatModule(ChatService)
	ChatService:RegisterFilterMessageFunction(functionId, doFilter)
end

return runChatModule

Error:

DoMessageFilter Function 'editText' failed for reason: Chat.ChatModules.DTScript:20: attempt to index nil with 'DTValue'  -  Server - ChatService:396

Thanks if you have an awnser!

1 Like

Is the variable “DTValue” a boolean value?

yes, the variable “DTValue” is an boolean value.

Okay so, the error

Attempt to index nil with ‘DTValue’

means that speaker.Backpack is nil.
Try printing speaker and speaker:WaitForChild("Backpack")

This does not work it results the same error. This is the code i put:

	local DTValue = speaker:WaitForChild("Backpack").DTValue

If this is in a module script, i think you need to put it this way:

local runChatModule = {
    -- your variables and functions here
    a = 1,
    b = 2,
    function a()
    end),
    function b()
    end)
}

return runChatModule

The value is in the backpack of a player. Not a value within just the script. Due to its toggable via a GUI button.

Is the variable speaker nil?

I just tried to do a print function, It is nil but its a chat module (A module in the chatModuel folder under chat). How would I make it not nil?

So the speaker variable is always nil? If the speaker variable is not always nil, we can do the following

if DTValue ~= nil then
    if DTValue.Value == true then
       -- code
    end
end

You have to wait for the speaker FIRST before finding the Backpack.

local speaker = game:GetService("Players"):WaitForChild(speaker.Name)
local DTValue = speaker:FindFirstChild("Backpack").DTValue

The speaker is not nil for some reason i did speaker.Name which is nil so its just giving the same error. It cannot find the value its not the DTValue.Value == true.

Its now saying im missing a argument

 ▶ DoMessageFilter Function 'editText' failed for reason: Argument 1 missing or nil (x2)  -  Server - ChatService:396

Before when I commented out the value stuff it worked. Now its not.

local functionId = "editText"

local function doFilter(speaker, messageObject, channelName)
	local random = Random.new()
	local letters = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}

	--function getRandomString(length, includeCapitals)
	--	local length = length or 10
	--	local str = ''
	--	for i=1,length do
	--		local randomLetter = letters[random:NextInteger(1,#letters)]
	--		if includeCapitals and random:NextNumber() > .5 then
	--			randomLetter = string.upper(randomLetter)
	--		end
	--		str = str .. randomLetter
	--	end
	--	return str
	--end

	local speaker = game:GetService("Players"):FindFirstChild(speaker.Name)
	local DTValue = speaker:WaitForChild("Backpack").DTValue
	if DTValue.Value == true then
		print("Player and DT True")
		local length = string.len(messageObject.Message)
		local str = ''
		for i=1,length do
			local randomLetter = letters[random:NextInteger(1,#letters)]
			if random:NextNumber() > .5 then
				randomLetter = string.upper(randomLetter)
			end
			str = str .. randomLetter
		end
		messageObject.Message = str
	end
end

local function runChatModule(ChatService)
	ChatService:RegisterFilterMessageFunction(functionId, doFilter)
end

return runChatModule

This is the script.

Here’s a new script because I think the first one got mixed up.

local speaker = game:GetService("Players"):WaitForChild(speaker.Name)
local DTValue = speaker:FindFirstChild("Backpack").DTValue

It still gives the same error. Even with that changed. Its missing an argument and I’m not sure what that argument is.

I think the ‘speaker’ void is the Missing Argument, mind checking some errors on the script that calls that function.

‘speaker’ is not void due to when i do print(speaker) it prints my roblox username. And then theres no errors from the script due to it is roblox’s ChatService script that is giving the warning that i have posted but it is also below.

DoMessageFilter Function 'editText' failed for reason: Argument 1 missing or nil  -  Server - ChatService:396

Do you mean print(speaker.Name)? If not then, try this.

local speaker = game:GetService("Players"):WaitForChild(speaker)
local DTValue = speaker:FindFirstChild("Backpack").DTValue

It works! Thank you.

I have another question if thats fine.

Is there a way for it to only show the scrambled text to some users. (If your in a group it does not show scrambled text if your not in the group it shows scrambled text)

I think you’re looking for if player:IsInGroup(xxxxxx) then
Here’s a developer hub article.