How to add variables in ModuleScripts?

So before you think I am dumb, I am talking about variables you add for example:

tool.Activated:Connect(function( this part is what im talking about )

I don’t know how to do it when using ModuleScripts, I am using a module script and I want to get the mouse position. But I don’t know how to add variables to module scripts.

I’ve tried to add variables in the module script because I thought since they are already connected, it might as well. But it didn’t work. I don’t think you would need the script for this, I just want to know how to add those variables.

I hope its not gibberish. Anything is appreciated.

1 Like

I’m not really sure what you’re trying to do here.

If you want to define a variable in your module that’s accessible to other scripts, add the variable to the table that’s returned:

local myClass = {}

myClass.apples = 10

return myClass

--//You can index apples from where this module script gets loaded 
1 Like

Well, first of all i think you’re talking about arguments, this is returned by the executed function.
These are passed when you first execute a function, for instance.

function abc.DoSomething(what) <-- argument received
      print(what)
end

abc.DoSomething("Print_Eat") <-- this right here is the argument passed
1 Like

So you know when you do a function like, here is an example

tool.activated:Connect(function( This is the variable part im talking about )

I want to add that but for a module script, because I want to print a mouse position from a module script. I am adding explosions and I thought that Modulescripts would be cleaner to use. And less resourceful.

MousePos still prints nil when I use that, here lemme send my scripts.:

Script I am calling it from:

local tool = script.Parent
local OnActivated = tool:WaitForChild("OnActivated")

local FireModule = tool:WaitForChild("FireModule")

OnActivated.OnServerEvent:Connect(function(plr, gunPos, mosPos)
	local myModule = require(FireModule)
	
	myModule.myFunc()
end)

Module Script (when I used your example):

local module = {}

local tool = script.Parent

module.myFunc = function(plr, gunPos, mosPos) -- where i thought you were talking about
	print("get nerdbodded idiot")
	tool:WaitForChild("Handle").Fire:Play()
	tool:WaitForChild("OnActivated").OnServerEvent:Connect(function(plr, gunPos, mosPos)
		print(gunPos)
		print(mosPos)
	end)
end
return module

You aren’t passing any arguments for myModule.myFunc().

1 Like

Thats because you aren’t passing the arguments (new code)

local tool = script.Parent
local OnActivated = tool:WaitForChild("OnActivated")

local FireModule = tool:WaitForChild("FireModule")


OnActivated.OnServerEvent:Connect(function(plr, gunPos, mosPos)
	local myModule = require(FireModule)
	
	myModule.myFunc(plr, gunPos, mosPos)
end)
2 Likes

I’ve tried doing that, it prints the position as nil, so I know its working. Its just printing the actual pos.

Could you please rephrase that?

Are you saying that myModule.myFunc(plr, gunPos, mosPos) still prints out nil? If so, that means the data you’re getting from the clientscript firing the event is also nil or empty.

Then I think you the data you’re passing from client to server in the remote event is nil, try reviewing that.

1 Like

Yea basically. Sorry my explanations arent the best… I will send the local script.

local tool = script.Parent

local Module = tool:WaitForChild("FireModule")

local OnActivated = tool:WaitForChild("OnActivated")

local plr = game:GetService("Players").LocalPlayer

local mouse = plr:GetMouse()

mouse.Button1Down:Connect(function(mouse)
	
	
	OnActivated:FireServer()
end)
1 Like

Yeah exactly, you aren’t passing anything in the FireServer function try passing the arguments you want there.

1 Like

This part here OnActivated:FireServer(passSomething)

2 Likes

Redlines whenever I add it there, I can only add it in the server script for some reason. The local script wont let me.

Screenshot it.

(too little characters)

1 Like

Well that’s because gunPos and mosPos isn’t a variable…

Please learn more about luau or Roblox coding in general, if you knew at least basic coding, you would know that gunPos and mosPos has to be initiated or assigned to at least something. In this case, you’re assigning the parameter with “gunPos” and “mosPos” which doesn’t exist at all.

If you’re new to scripting, start small. What you’re dealing with right now is pretty complex. Practice with small stuff, then gradually work on bigger and bigger projects.

2 Likes

Update:

A little harsh, sorry I get a little sensitive. Sorry for the off topic. But anyway I think I fixed it? I added a variable getting the mouse’s position. So the comment did help, I realized I needed to make a variable for it.

Thanks for helping.

1 Like

I’d say I am getting into pretty advanced stuff, I am learning the basics. I was just tired so I think that’s the reason why I didn’t realize it. I know most of the basics and I think I’ve mastered it.

1 Like
mouse.Button1Down:Connect(function()
	OnActivated:FireServer(plr, tool.Handle.Position, mouse.Hit.Position)
end)

The “.Button1Down” event of the mouse object doesn’t have any parameters. If I understood correctly I believe you were just trying to send the tool’s handle’s position and the mouse’s position to the server (when a mouse click occurred).

1 Like