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.
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).
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.
script.Parent here is referring to ServerScriptService, because the modules parent is ServerScriptService. I would recommend doing what @Syntheix said to prevent this.
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)
-- 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)
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