How would I return a value from events?

module:

local input = {}

local plr = game.Players.LocalPlayer
	local mouse = plr:GetMouse()

local returnInput = function()
	mouse.Button1Down:Connect(function()
		return true
	end)
end

input.checkInput = function()
	return returnInput()
end

return input

local script:

local inputModule = require(script.Input)
local inp_Bool = inputModule.checkInput()
print(inp_Bool )

It returns nil every time I print the bool.Am I missing something?

1 Like

Hi, is the local script the parent of the module script?

You couldn’t do it this way, because that value would be returned internally to where the event listener was called from, which doesn’t handle returned values at all.

If you’re trying to yield until a button is pressed, you could do:

mouse.Button1Down:Wait()
2 Likes

Yeah
(((((((((((((((((((((((((((((((((((( < char limit

If you’re trying to return if the player has their mouse down, in your local ReturnInput function, just do this

local returnInput = function()
	return game:GetService("UserInputService"):IsMouseButtonPressed(Enum.UserInputType.MouseButton1)
end

Or scrap the returnInput and just put it with checkInput instead since it would be a bit unneeded to use that function in this case

Or if you’re wanting to wait until the player’s mouse is down, do what @C_Sharper mentioned

Edit: Usually just for organization sakes, I would recommend putting game:GetService("UserInputService") in a variable if you plan to use it more than once, since it has events and functions for handling input

4 Likes

Ah,I see.Thanks @EmbatTheHybrid @C_Sharper

1 Like

Try this:

Module:

local input = {}

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

input.returnInput = function()
	mouse.Button1Down:wait()
	return true
end

input.checkInput = function()
	return input.returnInput()
end

return input

Localscript:

local inputModule = require(script:WaitForChild('ModuleScript'))
local inp_Bool = inputModule.checkInput()
print(inp_Bool)

This should fix all your problems.

1 Like

That’s the same thing C_Sharper mentioned about yielding/waiting till the mouse is down, also :Wait() is preferred over :wait() since the latter is deprecated

1 Like

I also changed some syntax, like adding input. to some things so it wouldn’t error.

Thank you too, @Grayseon

30000