How do I actually implement an ontouched function (player touch register) into a modulescript?

I’m making a form of tycoon but I’m unsure of how to actually implement my function into the modulescript so that I can require it through my actual button scripts. I have genuinely little to no experience with how modulescripts work and even tried to look up how to do it with no success (which is partially why I stopped developing the tycoon for a while) so I finally came here to ask for help on this. Here is my current code in a nutshell:

Modulescript (in serverscriptservice):

local module = {}

module.Function1 = function onTouched(hit)
-- code stuff here
end
script.Parent.Touched:connect(onTouched)

return module

and here’s the button script:

local modulerequire = require(game.ServerScriptService.ButtonModule)

modulerequire.Function1()

Genuinely super confused, if anyone could help me on this that would be great.

1 Like

What are you exactly trying to do?

I’m trying to load a module function for tycoon buttons that fulfills the functionality of said buttons (sensing whether or not the player touched the button and then going through the additional stuff like if they have enough in-game currency via leaderstats, if the button is for multiplying the in-game currency earnings, etc).

1 Like

change this line to

function module.OnTouched(hit)

or

function module.Function1(hit)

whichever name you prefer to use. You could even do

module.Function1 = function(hit)

Also, if that’s not the problem (I assume it is, but this other one is part of the problem too)

This would be a problem because 1, the function is actually being called “module.Function1” here, so

script.Parent.Touched:connect(module.Function1)
1 Like

The main issue here is the circumstance that the module is failing to even load into the button script.

1 Like

You can also do

game.Workspace.Part.Touched:Connect(function(hit) 
	modulerequire.Function1(hit) 
end)

Is the name for the module script correct?

Yep
image

1 Like

Where is the button script located?

the button script is located here
image

1 Like

What is this part of the code used for?

Are there any underlined errors in any of the scripts?

local mod = require(path)
local part = script.Parent
part.Touched:Connect(mod.func)

Define the function in your module script.

There are no references to the button when making the module script besides the function so you can’t call a touched event outside of said function.

2 Likes

To be short and frank if it makes it any clearer, I just ended up copy and pasting my code that I used for all the button scripts (and relocated some of the variables into the function since they were outside it initially) and dumped it into the modulescript. the script.Parent.Touched:connect(onTouched) line is just the remnant for when the code is supposed to execute when something touches the button.

1 Like

Found the issue.

script.Parent here is referring to ServerScriptService, because the modules parent is ServerScriptService. I would recommend doing what @Syntheix said to prevent this.

1 Like
script.Parent.Touched:connect(onTouched)

I don’t think it is needed, since your button script is not in SSS. And like what @Syntheix said about it.

I think this may solve your problem, I don’t know.

-- // Module Script \\
local module = {}

function onTouched(hit)
-- code stuff here
end

return module

-- // Script \\ --
local part = script.Parent
local module = require(game.ServerScriptService.ButtonModule)

part.Touched:Connect(function(hit)

module.onTouched(hit)

end)
1 Like

Can be reduced to

local module = require(game.ServerScriptService.ButtonModule)
part.Touched:Connect(module.onTouched)

Correct me if I’m wrong but you’ll error because you can’t access SSS locally. Try ReplicatedStorage

1 Like
-- Module Script
local module = {}

function module.onTouched(hit)
	-- code stuff here
end

return module

-- Script
local part = script.Parent
local modulerequire = require(game.ServerScriptService.ButtonModule)

game.Workspace.Part.Touched:Connect(function(hit) 
	modulerequire.onTouched(hit) 
end)

You can try this.

Well I definitely think I got closer to solving the issue with this code example but now somehow the variables aren’t even referencing the correct things… (also I tried changing the location of the modulescript to replicatedstorage and edited code accordingly, didn’t change anything)

Module script right now:

local module = {}

function module.onTouched(hit)
	
	local debounce = 0
	local price = script.Parent.Parent.Configuration.Price.Value
    --other code
end

return module

button script right now (note that these variables are repasted in both this and the modulescript to make sure the modulescript doesn’t see them as nil values and so that the button has the correct visual configs):

local price = script.Parent.Parent.Configuration.Price.Value

local config = script.Parent.Parent.Configuration

local buytargetname = script.Parent.Parent.Configuration.ObjectName.Value

local checkpoint = script.Parent.Parent.Configuration.Checkpoint.Value

local cmin = config.ButtonNumMin.Value

local cmax = config.ButtonNumMax.Value

local text1 = script.Parent.Parent.TextGui.Text1Sur.Text

local text2 = script.Parent.Parent.TextGui.Text2Sur.Text

text1.Text = ("Buy "..buytargetname.." for "..price)

text2.Text = ("Buy "..buytargetname.." for "..price)

local modulefort = require(game.ServerScriptService.ButtonModule)

script.Parent.Touched:Connect(function(hit)

modulefort.onTouched(hit)

end)

this is the error I’m getting in regards to the price variable

1 Like