So, I am making a Health Pick up thing and discovered recently that module scripts can be used multiple times throughout the different parts and/or scripts. But I don’t think I fully have the grasp of it but a friend did show me earlier how to do module scripts but he is currently busy with something so I would like to know how to fix my issue.
Here is the normal script code
local serverScriptService = game:GetService("ServerScriptService")
local getModule = serverScriptService.ModuleScript
require(getModule)
And here is the Module script code
local hitBox = {}
function hitBox.box(Part)
local getPart = Part.Parent
local getUnion = getPart.Union
local coolDown = 3
local myDebounce = true
hitBox.Touched:Connect(function(otherPart)
if myDebounce == true then
myDebounce = false
print("Hit detected")
local parentPart = otherPart.Parent
local humanoid = parentPart:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health = humanoid.Health - 50
print("Removed health")
getUnion.Transparency = 1
wait(coolDown)
getUnion.Transparency = 0
end
myDebounce = true
end
end)
end
return hitBox
And here is what my workspace looks like:
I made a invisible hitbox around the union which detects the collision between the player and the hitbox and it just makes the union transparent.
(BTW Don’t mind that the health pick up takes away health, I used that for testing purposes)
local serverScriptService = game:GetService("ServerScriptService")
local getModule = require(serverScriptService.ModuleScript) -- Returns a required ModuleScript (essentially a table)
getModule.box(--[[part that you want to pass through]]) --Call the box method of the getModule table
Also, in your box method, you write:
function hitBox.box(Part)
local getPart = Part.Parent
local getUnion = getPart.Union
In your explorer, however, I don’t see any part that you could use to pass through the parameters, besides the union itself (which would mean the third line there is redundant code). Anyway I’m sure you can get everything sorted out.
Hopefully this helps. Let me know if you have any further questions.
I am currently at work but I remember my friend showing me similar layout of writing the Module so thank you for the reminder and ofc the code. Bless you have a good day!
I really think you should learn how to use ModuleScripts to achieve object-oriented design (OOD). Object-oriented programming (OOP) is a game-changing approach for tackling many programming situations. It’s a powerful and adaptable strategy that can be easily replicated with ModuleScripts.
Thank you, I was more thinking of expanding my general knowledge on OOP due to me being in first year of my computer science degree, these links will come in handy!