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!
_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.
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!"
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.