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:
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)
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.
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)
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)
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.
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.