Module Script Help Please!

Hi, I am having a problem (What else is new) with module scripts. I have this in a module script

local module = {}

_G.SHOOTBI = function(Character)
	print("Connected!")
	local bplane = script.Parent
	local arr = bplane:GetChildren()
	for _,v in pairs(arr) do
		if v.Name == "MachineGun" then
			print("Da Script Haz Been Run")
			local Part = Instance.new("Part")
			local Plane = script:FindFirstAncestor("Blue Interceptor")
			local Player = Plane.Parent
			Part.BrickColor = BrickColor.new("Navy blue")
			Part.Name = "Bullet"
			Part.Material = Enum.Material.Neon
			Part.CanCollide = false
			Part.FormFactor = "Symmetric"
			Part.Size = Vector3.new(10,10,6)
			Part.BottomSurface = "Smooth"
			Part.TopSurface = "Smooth"
			local Mesh = Instance.new("BlockMesh")
			Mesh.Parent = Part
			Mesh.Scale = Vector3.new(1/25,1/25,1)
			local BV = Instance.new("BodyVelocity")
			BV.Parent = Part
			BV.maxForce = Vector3.new(math.huge,math.huge,math.huge)
			local PlaneTag = Instance.new("ObjectValue")
			PlaneTag.Parent = Part
			PlaneTag.Name = "PlaneTag"
			PlaneTag.Value = Plane
			local effective = math.random(1,21)
			print(effective)
			Part.Touched:connect(function(Object)
				if (not Object:IsDescendantOf(Character)) then
					local redSpaceplane = Object:FindFirstAncestor("Red Spaceplane")
					local redInterceptor = Object:FindFirstAncestor("Red Interceptor")
					local HitHumanoid = Object.Parent:findFirstChild("Humanoid")
					if effective == 5 and (redSpaceplane or redInterceptor) then
						Object:Destroy()
						print(effective)
						print("successful hit!")
						print(Object.Name)
					end
				end
			end)
			Part.Parent = game.Workspace
			Part.CFrame = v.CFrame + v.CFrame.lookVector * 10
			BV.velocity = (v.Velocity) + (Part.CFrame.lookVector * 2000) + (Vector3.new(0,0.15,0))
			delay(3,function() Part:Destroy() end)
		end
	end
	wait(0.25)	
end




return module

And I have this to run it. (LocalScript)

function FireMachineGun(GunParent)
	_G.SHOOTBI()
end

Returned Attempt to call a nil value.
Help please!

I think the issue is that you have to put

module.SHOOTBI

instead of

_G.SHOOTBI

Because _G was never initialized anywhere in your module script.

Also make sure to import the module script if it still does not work.

2 Likes

Hello. This is it now.

_G.SHOOTBI = function(Character)
	print("Connected!")
	local bplane = script.Parent
	local arr = bplane:GetChildren()
	for _,v in pairs(arr) do
		if v.Name == "MachineGun" then
			print("Da Script Haz Been Run")
			local Part = Instance.new("Part")
			local Plane = script:FindFirstAncestor("Blue Interceptor")
			local Player = Plane.Parent
			Part.BrickColor = BrickColor.new("Navy blue")
			Part.Name = "Bullet"
			Part.Material = Enum.Material.Neon
			Part.CanCollide = false
			Part.FormFactor = "Symmetric"
			Part.Size = Vector3.new(10,10,6)
			Part.BottomSurface = "Smooth"
			Part.TopSurface = "Smooth"
			local Mesh = Instance.new("BlockMesh")
			Mesh.Parent = Part
			Mesh.Scale = Vector3.new(1/25,1/25,1)
			local BV = Instance.new("BodyVelocity")
			BV.Parent = Part
			BV.maxForce = Vector3.new(math.huge,math.huge,math.huge)
			local PlaneTag = Instance.new("ObjectValue")
			PlaneTag.Parent = Part
			PlaneTag.Name = "PlaneTag"
			PlaneTag.Value = Plane
			local effective = math.random(1,21)
			print(effective)
			Part.Touched:connect(function(Object)
				if (not Object:IsDescendantOf(Character)) then
					local redSpaceplane = Object:FindFirstAncestor("Red Spaceplane")
					local redInterceptor = Object:FindFirstAncestor("Red Interceptor")
					local HitHumanoid = Object.Parent:findFirstChild("Humanoid")
					if effective == 5 and (redSpaceplane or redInterceptor) then
						Object:Destroy()
						print(effective)
						print("successful hit!")
						print(Object.Name)
					end
				end
			end)
			Part.Parent = game.Workspace
			Part.CFrame = v.CFrame + v.CFrame.lookVector * 10
			BV.velocity = (v.Velocity) + (Part.CFrame.lookVector * 2000) + (Vector3.new(0,0.15,0))
			delay(3,function() Part:Destroy() end)
		end
	end
	wait(0.25)	
end

Runner:

function FireMachineGun(GunParent)
	_G.SHOOTBI()
end

As Ladea said, you’re attempting to add your function to a dictionary called _G, your module script’s dictionary is called module. Thus, when you try to fetch your function from a dictionary that doesn’t exist, it’ll return nil.

2 Likes

Please don’t use _G!

_G is the root of all evil.


Imagine the contents of a ModuleScript like a big function. When you require a ModuleScript you are running the function and getting the result. This result is cached for scripts within the same VM.

-- ModuleScript
local module = {}
function module.DoStuff()
    print("Hello!")
end
return module
-- Script
local Module = require(path.to.module)
Module.DoStuff() -- will print "Hello!"
1 Like

What is path.to.module? 30 kars

The path to the module? If the module was in the Workspace then you would use require(workspace.Module) where Module is the name of the ModuleScript instance.

1 Like

It gives an error. There is a blue line under path.

function FireMachineGun(GunParent)
	local Module = require(path.to.module)
	Module.DoStuff() 
end

You are not meant to copy my code. It is an example. Please read this article on ModuleScripts.

1 Like

I suggest taking a little bit more time understanding how Roblox Lua works like @busterbob64 said. Packages or an article on ModuleScripts.

I recommend both readings, ModuleScripts (or modules) are essentially just lua packages integrated into Roblox.

local module = {}

module.shootbi = function(char)
    ...
end

return module
local varname = require(...)

function FireMachineGun(GunParent)
   varname.shootbi()
   ...
end

Replace path.to.module with wherever you’re ModuleScript is.

Example:
game:GetService(“ReplicatedStorage”).module_script

this is just an example!!