First time using ModuleScripts, need help with something

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:

image_2022-11-30_163049414

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)

You have to call the hitBox.box(Part) method.

Script code:

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.

1 Like

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!

Just an additional suggestion.

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.

If you’re not familiar with OOP, here’s a nice, short video that explains it:
Fundamental Concepts of Object Oriented Programming

Here are some helpful DevForum links that explain how to implement OOP in Luau:
Roblox OOP (Object Oriented Programming)
All about Object Oriented Programming

I highly suggest you invest some time into researching into this. This is where the true power of ModuleScripts are unlocked.

2 Likes

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!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.