Can anyone give me a explanation in how to use ModuleScript properly?

So, i started messing around with module scripts to save myself from future pain, but i am not getting the correct idea in how to apply it.

  • What is the purpose of what is happening and what i want to do:

I have this TurboPad which has the following script inside:

image

Script:

local humanoid = script.Parent.Touched:Wait("Humanoid")
local plr = humanoid.Parent
local module = require(game:GetService("ServerScriptService").TurboPad)

script.Parent.Touched:Connect(function(hit)
	module.TurboTouched(hit.Parent)
end)

Basically just calling the ModuleScript (probably not the right way help me D:) moving on…

Then we have the ModuleScript with the following inside:

local module = {}
    function module.TurboTouched(hit) 
	local h = hit.Parent:FindFirstChild("Humanoid")
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if (h ~= nil) and player:WaitForChild("PlayerInfo").GetBoost.Value == 0 then
		player.PlayerInfo.GetBoost.Value = 1
	end
	end
	
return module

The purpose here is to detect if a player humanoid touched the part and if it’s not nil and it’s IntValue for GetBoost is 0 then it will be changed to 1 (eventually giving the player who touched the part the boost).

But as the way as the code currently is, i am getting no results and no errors on the output neither, if anyone can give me a better insight on this i’ll be very thankful.

  • Note: The code for giving the boost and stuff was working fine before i started messing with ModuleScript (before anyone asks)

  • The ModuleScript is located in ServerScriptServer

1 Like

ModuleScripts are scripts that are used to contain large amounts of data(preferably a table) that can be accessed an edited by other scripts. What I’m looking at from your ModuleScript, you’d probably be best off using a script instead because using a script without the module table because a ModuleScript is like creating an Instance… you would have your functions, values, properties, etc, and using a ModuleScript with your code is not correct.

For more info, please visit ModuleScript | Documentation - Roblox Creator Hub

1 Like

Hmm, is there a way to use a single script to do the same stuff in all TurboPads in this case? cause i don’t want to CopyPaste the scripts every new TurboPad i clone (if i changed something inside the script i would have to change it on the other ones aswell and i am trying to avoid that)

1 Like

The page you’ve linked says the following:

ModuleScripts are essential objects for adhering to the don’t-repeat-yourself (DRY) principle. When you write a function, write it only once and use it everywhere. Having multiple copies of a function is disastrous when you need to change that behavior. So, you should define functions or groups of functions in ModuleScripts and have your Scripts and LocalScripts call require on your ModuleScripts. Keep your code organized! (emphasis added)

@ImparuZ

local humanoid = script.Parent.Touched:Wait("Humanoid")
local plr = humanoid.Parent

What is the purpose of this code? I can’t seem to find documentation for a :Wait() method on .Touched. Try removing those two lines and see if it works.

1 Like

Of course!
copy the TurboTouched function(DELETE THE MODULE PART) and put it into the first script. Then connect the touched event with turbotouched.

1 Like

you can do a wait function in an event … It will pause the script until the event executes at least once.

Ah, you’re correct. What I’m wondering is why it’s being called with an argument ("Humanoid").

Also, waiting for the Touched event to fire is somewhat redundant, considering that the script initiates a connection to the Touched event at the end.

That should really be a number…

Also you’d probably be best removing that humanoid variable in the script since it’s incorrect and you don’t need it

and plus he doesn’t even need the var, since he doesn’t use it in his script at all…

1 Like

From what I can see, your identifying the “hit” through the module.TurboTouched function as the Parent of the original hit instance, thus “hit.Parent:FindFirstChild(“Humanoid”)” will return nil.

If my hunch is correct, this is a very simple fix, new code:

local module = {}
    function module.TurboTouched(hit) 
	local h = hit:FindFirstChild("Humanoid")
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if (h ~= nil) and player:WaitForChild("PlayerInfo").GetBoost.Value == 0 then
		player.PlayerInfo.GetBoost.Value = 1
	end
	end
	
return module

To look at redundancy however:
local humanoid = script.Parent.Touched:Wait("Humanoid") is entirely redundant and I would advise against it.

On top of that, I would remove local plr = humanoid.Parent seeing as this is unnecessary and is not used anywhere else in your .Touched function.

1 Like

No, that’s not what i meant, what i want is to use only 1 script and call this script to all Turbopads in the game

Tried that, no results and no errors on the output :confused:

Problem solved, just moved

if (h ~= nil) 

To the TurboPad touched script:

script.Parent.Touched:Connect(function(hit)
	local h = hit.Parent:FindFirstChild("Humanoid")
	if (h ~= nil) then
	module.TurboTouched(hit)
	end
end)