Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I return values in a Module Script?

Module Script, located in Workspace

local module = {}

function module.ReceiveChat(Message, Sender)
	return Message, Sender
end

function module.SendChat(Message, Sender)
	if Message ~= nil and Message ~= "Click here or press '/' to chat!" then
		module.ReceiveChat(Message, Sender)
	end	
end

return module


Server Script, located in StarterGui under a GUI

local module = require(game.Workspace.ModuleScript) --486793676
local player = script.Parent.Parent.Parent
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
	if key:byte() == 13 then
		if script.Parent.EnterText ~= "Click here or press '/' to chat!" and script.Parent.EnterText ~= "" and script.Parent.EnterText ~= nil then
			module:SendChat(script.Parent.EnterText.Text, player.Name)
		end
	end
end)

game:GetService("UserInputService").InputBegan:connect(function(key)
	if key.KeyCode == Enum.KeyCode.Slash then
		script.Parent.EnterText:CaptureFocus()
	elseif key.KeyCode == Enum.KeyCode.BackSlash then
		module.ReceiveChat:connect(function(Message, Sender) --Error right here
			print(Message .. ", " .. Sender)
		end)
	end
end)

It sends the chat through, but whenever I try to get it back, it gives me this error: "attempt to index field 'ReceiveChat' (a function value)"

I really don't know how to make it so I can get the values back. Help is very appreciated, thanks!

1 answer

✓ Accepted answer

Events

First, I want to ask what made you think that was a good way to implement events, and why you thought that would act like an event at all. I want you to really think about this, because it'll make sure that you don't make the mistake again.


Roblox Events
Native, nasty

You can try and use Roblox Bindables for this. It's nasty, but it'll probably do the job (For most things):

local Rcv = Instance.new "BindableEvent"
local module = {}
module.RecieveChat = Rcv.Event;

function module.SendChat(Message, Sender)
    if Message ~= nil and Message ~= "Click here or press '/' to chat!" then
        Rcv:Fire(Message, Sender)
    end 
end

return module

Freya Events
Shameless advertising

Freya events are (Obviously) a component of Freya, designed to take over the role of Roblox BindableEvents as a non-Instance datatype. They don't have the same issues as Roblox Bindables, and have an extended API, but can't be Parented.

local Event = require(Freya.Events) -- Obviously the Events module goes here
local module = {}
module.RecieveChat = Event.new();

function module.SendChat(Message, Sender)
    if Message ~= nil and Message ~= "Click here or press '/' to chat!" then
       module.RecieveChat:Fire(Message, Sender)
    end 
end

return module

Nevermore Signals
Quenty owes me for this

Nevermore signals do a similar job to Freya's events, however they're not as efficient and use a different API. They can however be destroyed to clean them up.

local Event = require(Nevermore.Signal) -- Obviously the Signal module goes here
local module = {}
module.RecieveChat = Event.new();

function module.SendChat(Message, Sender)
    if Message ~= nil and Message ~= "Click here or press '/' to chat!" then
       module.RecieveChat:fire(Message, Sender)
    end 
end

return module

This discussion is preserved as it was. New answers, comments, and votes are closed.