Need help on making a player say a message on button click

I’m trying to make the player who clicks the continueBtn say “Hello, World!” in the Yellow Team chat channel, but I get this error output:

20:50:55.283 recieved - Client - Statements:2
20:50:55.283 StarterPlayer.StarterPlayerScripts.Statements:3: attempt to index nil with ‘SpeakerAdded’ - Client - Statements:3

I’m not sure how I could make this work, any help would be appreciated

Localscript:

local sayStatement = require(game.StarterPlayer.StarterPlayerScripts:WaitForChild("Statements"))
continueBtn.MouseButton1Click:Connect(function()
    sayStatement()
end)

Modulescript:

local function sayStatement(ChatService)
	print('recieved')
	ChatService.SpeakerAdded:Connect(function(playerName)
		local speaker = ChatService:GetSpeaker(playerName)
		speaker:SayMessage("Hello, World!", "Yellow Team") -- message: "Hello, World!", chat channel: Yellow Team
		print('said')
	end)
end

return sayStatement
2 Likes

This is because your trying to receive the parameter “ChatService” but it doesn’t get sent in the local script. Here are adjustments you can make:

local ChatService = game:GetService("ChatService")
local function sayStatement()
	print('recieved')
	ChatService.SpeakerAdded:Connect(function(playerName)
		local speaker = ChatService:GetSpeaker(playerName)
		speaker:SayMessage("Hello, World!", "Yellow Team") -- message: "Hello, World!", chat channel: Yellow Team
		print('said')
	end)
end

return sayStatement
2 Likes

I tried the method but it gave me this output:
21:54:17.355 SpeakerAdded is not a valid member of ModuleScript "Chat.ChatServiceRunner.ChatService" - Client - Statements:5

1 Like

Alright i’ve fixed it – For anyone viewing this in the future, i made a serverscript containing the text stuff and fired it on server whenever the textbutton was clicked. cheers.

sayMessageEvent.OnServerEvent:Connect(function(player)
	
	local speaker = ChatService:GetSpeaker(player.Name)
	print(speaker)
	
	speaker:SayMessage("Hello, World!", "Yellow Team")

end)
1 Like

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