Attempt to call a nil value

Hello, I have encountered a problem in my script that won’t allow me to continue writing code. I have a local script in StarterPlayerScripts:

local chatService = game:GetService("TextChatService")
local MessageEvent = game.ReplicatedStorage.FetchMessage -- remote event

chatService.OnIncomingMessage = function(message)
	MessageEvent:FireServer(game.Players.LocalPlayer, tostring(message.Text))
end

RemoteEvent “FetchMessage” passes the message text to the server script in the ServerScriptService:

local module = require(game.ServerScriptService.Functionality)
local chatService = game:GetService("TextChatService")
local MessageEvent = game.ReplicatedStorage.FetchMessage

local CheckInputForAnswer = function(input, index)
	local part = module.Functions.FindIndex(index)
	print(part()) -- This is where the error appears, line 51
	if input == module.Functions.DecodeData(part():FindFirstChild("Answer").Value) then
		return true
	else
		return false
	end
end

MessageEvent.OnServerEvent:Connect(function(player, player2, message)
	if player then
		if CheckInputForAnswer(message, player2:FindFirstChild("miscValues").StageIndex.Value + 1) == true then -- Line 61
			module.Functions.RemoveBorder(player2:FindFirstChild("miscValues").StageIndex.Value + 1)
		end
	end
end)

The server script uses some functions from the module script, also located in the ServerScriptService:

local tweenService = game:GetService("TweenService")
local Colours = {
	Color3.fromRGB(244, 68, 46),
	Color3.fromRGB(185, 95, 137),
	Color3.fromRGB(76, 224, 210),
	Color3.fromRGB(131, 103, 199),
	Color3.fromRGB(44, 42, 74),
	Color3.fromRGB(69, 203, 133),
	Color3.fromRGB(94, 124, 226),
	Color3.fromRGB(255, 178, 56),
}

local module = {
	Functions = {
		DecodeData = function(data)
			data = string.gsub(data, '[^'..b..'=]', '')
			return (data:gsub('.', function(x)
				if (x == '=') then return '' end
				local r,f='',(b:find(x)-1)
				for i=6,1,-1 do r = r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
				return r;
			end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
				if (#x ~= 8) then return '' end
				local c=0
				for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
				return string.char(c)
			end))
		end,
		FindIndex = function(index)
			local part = function() 
				for i,v in pairs(workspace.Map.ImageBoards:GetDescendants()) do
					if v:IsA("NumberValue") and v.Value == index then
						return v.Parent
					end
				end
			end
		end,
		RemoveBorder = function(index)
			local part = function() 
				for i,v in pairs(workspace.Map.ImageBoards:GetDescendants()) do
					if v:IsA("NumberValue") and v.Value == index then
						return v.Parent:FindFirstChild("Part")
					end
				end
			end
			local randomColour = Colours[math.random(1, #Colours)]
			part().Color = randomColour
			part().CanCollide = false
			part().Star.Enabled = true
			local partCurrentSize = part().Size
			tweenService:Create(part(), TweenInfo.new(1.5, Enum.EasingStyle.Circular), {Transparency = 1, Size = partCurrentSize + Vector3.new(1.5, 0, 1.5)}):Play()
			tweenService:Create(part().SurfaceGui.ImageLabel, TweenInfo.new(1.5, Enum.EasingStyle.Circular), {ImageTransparency = 1}):Play()
			part().Correct:Play()
			for i,v in pairs(part():GetChildren()) do
				if v:IsA("Texture") then
					tweenService:Create(v, TweenInfo.new(1.5, Enum.EasingStyle.Circular), {Transparency = 1}):Play()
				end
			end
			wait(1)
			part().Star.Enabled = false
		end,

	},
}
	
return module

When sending a message to the chat, this errors occurs:

ServerScriptService.Handler:51: attempt to call a nil value  -  Server - Handler:51
Stack Begin  -  Studio
Script 'ServerScriptService.Handler', Line 51  -  Studio - Handler:51
Script 'ServerScriptService.Handler', Line 61  -  Studio - Handler:61
Stack End  -  Studio

The error occurs in the server script, I’ve marked where exactly. Any help would be appreciated!

The parameter tuple () you have on part attempts to call part.

According to the definition of part, part is defined to be whatever comes from some utility function lying around somewhere.

Inspecting the utility function, you can see it only returns a value in a single codepath, where the condition holds that v matches class NumberValue and the Value field of the object is equal to the provided index parameter; in every other case the function returns no value, which means you get nil in the slot. None of this is probably what you’d have liked to do anyways, as Instance, like nil, is also not callable.

1 Like

I fixed the error with print(), but it went to the next line, and now says:

ServerScriptService.Handler:52: attempt to index nil with 'FindFirstChild'  -  Server - Handler:52
Stack Begin  -  Studio
Script 'ServerScriptService.Handler', Line 52  -  Studio - Handler:52
Script 'ServerScriptService.Handler', Line 61  -  Studio - Handler:61
Stack End  -  Studio

How do you suggest rewriting the scripts to get rid of this issue?

You treated the symptom not the underlying problem. The error is not just a piece of annoying text that shows up to ruin your day, it’s telling you you made a mistake in your code. I encourage you to reread my response.

I found a solution, thank you so much!

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