Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to call a variable using module scripts?

Asked by
Galicate 106
6 years ago

Title is confusing... So i created variables in a module script and I want to call them up in a localscript.

this is what i got but now when i fire the gun it doesnt fire and says FireRate isnt a member of settings. Settings is the name of the module script and FireRate is the name of the variable.

--module script
local module = {}

gunType = {
        Semi = false;
        Auto = true;
        Burst = false;
        Shot = false;
        Explosive = false;
};

Keys = {
        Crouch = "c";
        Prone = string.char(50);
        Reload = "r";
        Sprint = string.char(48);
};

Damage = math.random(20,70)

ReloadTimes = 1; --Time it takes for the gun to reload.
ReloadAnimation = 010101010; --ID of the animation.

FireRate = 0.1; --Time between each shot.

return module
--Local Script/Receiver script
-----------Settings Variables----------
local Settings = script.Parent.Settings
local FireRate = Settings.FireRate
local Damage = Settings.Damage
local ReloadTimes = Settings.ReloadTimes
local ReloadAnimation = Settings.ReloadAnimation
---------------------------------------
local Player = game.Players.LocalPlayer
local mouse_hold = true
local Character = Player.Character or Player.CharacterAdded:wait()
local Camera = workspace.CurrentCamera
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local Ammo = script.Parent:WaitForChild("Ammo")
tool.Equipped:connect(function(mouse)
print("Tool equipped!")
holdanim = player.Character.Humanoid:LoadAnimation(script.Parent.HoldAnim)
reloadanim = player.Character.Humanoid:LoadAnimation(script.Parent.ReloadAnim)

 mouse.Button1Down:connect(function()
        if Ammo.Value >= 1 then
        print("Mouse pressed!")
        Ammo.Value = Ammo.Value - 1
        print(Ammo.Value)
        script.Parent.Handle.Fire:Play()
        local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
        local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

        local beam = Instance.new("Part", workspace)
        beam.BrickColor = BrickColor.new("Gold")
        beam.FormFactor = "Custom"
        beam.Material = "Neon"
        beam.Transparency = 0.99
        beam.Anchored = true
        beam.Locked = true
        beam.CanCollide = false

        local distance = (tool.Main.CFrame.p - position).magnitude
        beam.Size = Vector3.new(0.3, 0.3, distance)
        beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)


        game:GetService("Debris"):AddItem(beam, 0.1)

        if part then
            local humanoid = part.Parent:FindFirstChild("Humanoid")

            if not humanoid then
                humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
            end

            if humanoid then
                humanoid:TakeDamage(math.random(10,30))

            end
        end
        end
wait(FireRate)
        end)
end)

local Player = game.Players.LocalPlayer
local action_reload = false
Mouse = Player:GetMouse()

Mouse.KeyDown:connect(function(Key)
    if(Key:lower() == "r") and Ammo.Value < 7 and action_reload == false then
        action_reload = true
        script.Parent.Handle.Reload:Play()
        reloadanim.Looped = false
        reloadanim:Play()
        wait(2)
        Ammo.Value = 7
        print(Ammo.Value)
        action_reload = false
    end
end)

function onEquipped()
    holdanim:Play()
end

script.Parent.Equipped:connect(onEquipped())
mouse = Player:GetMouse()
action_aim = false
mouse.Button1Down:connect(function()
    if action_aim == false and action_reload == false then
        action_aim = true
print("trying to aim")
       repeat wait()
    Camera.CameraType = Enum.CameraType.Scriptable
until Camera.CameraType == Enum.CameraType.Scriptable
Camera.CFrame = script.Parent.AimPart.CFrame
print("aiming")
    end
end)

1 answer

Log in to vote
0
Answered by
mraznboy1 194
6 years ago
Edited 6 years ago

When you're using a module script, you're just storing all the variables/functions into a table then returning that table to be used in other script. When you're using the module script in other scripts, you need to use the function require() to access the table. Take for example:

--MODULE SCRIPT in a script named ModuleExample in the workspace
local module = {}

function module:AddNumbers(a,b)
    return a+b
end

module.Table = {
    "Example Value",
    SecondValue = 5
}

module.ExampleVariable = "Variable"

return module --< this line is key in making sure you can access these variables
--SCRIPT 
local importedModule = require(workspace.ModuleExample)

local sum = importedModule(3,4)
print(sum)
print(importedModule.Table[1]
print(importedModule.Table.SecondValue)
print(importedModule.ExampleVariable)

We printed four things here, and our output would look something like this:

7

Example Value

5

Variable

Hope this clears things up.

Ad

Answer this question