Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How could I store variables in modules?

I've been trying to learn how to use modules, and, I attempted to use a table to store my variables. It keeps saying 'Attempted to index nil with "meditating"', and I don't know why it's happening, or where I went wrong in holding the variables. Code:

local module = {}
local var = {
    rs = game:GetService("ReplicatedStorage"),
    run = game:GetService("RunService"),
    cas = game:GetService("ContextActionService"),
    meditating = false,
    enrg = script.Parent.Parent:WaitForChild("Power")    
}


module.MedButton = function(action, inputState, var)
    if action == "meditating" then
        if inputState == Enum.UserInputState.Begin then
            var.meditating = true
            print(var.meditating)
        elseif inputState == Enum.UserInputState.End then
            var.meditating = false
            print(var.meditating)
        end
    end
end


module.medstepfunc = function(var)
    if var.meditating == true then
        var.enrg.Value = math.clamp(var.enrg.Value + 10*deltaTime, var.enrg.MinValue, var.enrg.MaxValue)
        print(var.enrg.Value)
    end
end
    
    
return module

Any explanations/help is appreciated

1 answer

I mean it's pretty self-explanatory, You're not even returning your desired module block. You would need to insert your variables into the Module pair of brackets

This discussion is preserved as it was. New answers, comments, and votes are closed.