Attempt to index nil with 'JoinChannel'

I keep getting this error, why? What can I do to fix this issue?

wait(1/30)
local SSS = game:GetService("ServerScriptService")
local CSM = require(SSS:WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))
-- Get user details --
local Username = "jaidon11"
local Speaker = CSM:GetSpeaker(Username)
local SuperSecretChannel = CSM:AddChannel("SuperSecretChannel")
Speaker:JoinChannel('SuperSecretChannel')

image

Make sure to handle GetSpeaker, it does not error.

You’re likely getting a nil return instead of the speaker.

I don’t understand, what do you mean by handle GetSpeaker?

You’re getting a nil error when attempting to use the function “JoinChannel”. Thus, Speaker must be null (nil, empty, doesn’t exist).

To “handle an exception” or error, as @metatablecatmaid mentioned, you must write you code Defensively.
This means, if some variable can be null (such as the results of WaitForChild, FindFirstChild, etc) you must handle the case in where that variable is null and doesn’t exist.

local Speaker = CSM:GetSpeaker(Username)
local SuperSecretChannel = CSM:AddChannel("SuperSecretChannel")
if (Speaker ~= nil) then -- Check if Speaker exists yet
	Speaker:JoinChannel("SuperSecretChannel")
else
	-- handle the case where Speaker doesn't exist.
	-- for example - figure out why Speaker doesn't exist and either attempt to create it again
end

Just a quick point to what your problem might be in this specific case - rather than waiting 1/30 of a second to run the above code, instead use the PlayerAdded event of Players so that you know the player definitely exists, if this is indeed the issue with GetSpeaker.

local SSS = game:GetService("ServerScriptService")
local CSM = require(SSS:WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))
local SuperSecretChannel = CSM:AddChannel("SuperSecretChannel")

game.Players.PlayerAdded:Connect(function(player) -- when a player joins the game...
	print(player,"joined the game!")
	-- Get user details --
	local Username = player.Name
	local Speaker = CSM:GetSpeaker(Username)
	if (Speaker ~= nil) then
		print(player,"is attempting to join the SuperSecretChannel!")
		Speaker:JoinChannel('SuperSecretChannel')
		print(player,"joined SuperSecretChannel!")
	else
		print(player,"could not join the SuperSecretChannel!")
	end
end)