Cant get the value from a Module Script

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want the script to get the value from a Module script

  2. What is the issue? Issue

22:37:38.253 Config - Client - ViewModelSway:14
22:37:38.255 ReplicatedStorage.Modules.ViewModelSway:14: attempt to index nil with ‘Config’ - Client - ViewModelSway:14
22:37:38.255 Stack Begin - Studio
22:37:38.255 Script ‘ReplicatedStorage.Modules.ViewModelSway’, Line 14 - function module - Studio - ViewModelSway:14
22:37:38.255 Script ‘Workspace.efsane14010.ViewModel’, Line 40 - Studio - ViewModel:40
22:37:38.255 Stack End - Studio

  1. What solutions have you tried so far? I couldnt find anything about this

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Guns = ReplicatedStorage.Guns
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local Camera = game.Workspace.CurrentCamera

local module = {}

local swayCFrame = CFrame.new()
local lastCameraCFrame = CFrame.new()

function module(gun: Model, ViewModel, config)

	local swaySize = config.swaySize
	
	local rot = Camera.CFrame:ToObjectSpace(lastCameraCFrame)
	local X, Y, Z = rot:ToOrientation()
	
	swayCFrame = swayCFrame:Lerp(CFrame.Angles(math.sin(X) * swaySize, math.sin(Y) * swaySize, 0), .1)

	lastCameraCFrame = Camera.CFrame

	return swayCFrame
end

return module

I mean it does get printed either way I do print(gun.Config) (config is in the gun) or print(config.swaySize)

1 Like

This shows that the error is from ReplicatedStorage.Modules.ViewModelSway on line 14. Would you mind providing that script as well?

1 Like

This is the script that gives error

1 Like

Sorry for the late reply.
Are you sure it’s the most up to date version? Line 14, where it says the error is, is empty.

1 Like

Have you done require(module)?

1 Like

Okay so, i tried recreating the issue, it seems to work fine when i do it this way:

LocalScript : “LocalScript” (StarterPlayerScripts) :


--[[

		Script Example

]]

--//LIBRARIES

local Module = require(script.Module)

--//DEFINE

local GunModel = nil
local ViewModel = nil

local config = {
	swaySize = 2
}

local swayCFrame = Module(GunModel,ViewModel,config)
print(swayCFrame)

--Rest of the code below...

ModuleScript : “Module” (Child Of LocalScript) :

local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Guns = ReplicatedStorage.Guns
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local Camera = game.Workspace.CurrentCamera

local module = {}

local swayCFrame = CFrame.new()
local lastCameraCFrame = CFrame.new()

function module(gun: Model, ViewModel, config)

	local swaySize = config.swaySize

	local rot = Camera.CFrame:ToObjectSpace(lastCameraCFrame)
	local X, Y, Z = rot:ToOrientation()

	swayCFrame = swayCFrame:Lerp(CFrame.Angles(math.sin(X) * swaySize, math.sin(Y) * swaySize, 0), .1)

	lastCameraCFrame = Camera.CFrame

	return swayCFrame
end

return module

As you can see in the screenshot i attached below, it prints the variable "swayCFrame " that is retrieved from the module.

Print Result:

File Structure:

The issue you might be having is that you arent reference a config when you call the function from the module. (Hence why it’s saying there was an teempt to index a nil object)

Hope this helps!

Yes I did. Sorry for late reply

LOCAL SCRİPT THAT CONTAİNS THE REQUIRE:

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local player = game:GetService("Players").LocalPlayer
local Camera = game.Workspace.CurrentCamera

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local EquipGun = require(game.ReplicatedStorage.Modules.EquipGun)
local ViewModelSway = require(game.ReplicatedStorage.Modules.ViewModelSway) -- THE PART I USE TO FIX THE ISSUE
local AimModule = require(ReplicatedStorage.Modules.Aim)

local Inventory = require(player.PlayerGui.SelectItems.Inventory)

local EquippedViewModelName = Inventory.Outfit
local EquippedGunName = Inventory.Primary.Name

local ViewModel = ReplicatedStorage.ViewModels:FindFirstChild(EquippedViewModelName):Clone()
ViewModel.Parent = Camera

local isAiming 
local AimCF = CFrame.new(0,-1.2, 0)

function Aim()
	local gun = ViewModel[EquippedGunName]
	local gunConfig = require(gun.Config)
	if gunConfig.canAim and isAiming and ViewModel ~= nil then
		local offset = gun.Aim.CFrame:ToObjectSpace(ViewModel.PrimaryPart.CFrame * CFrame.new(0,0,-.8))
		AimCF = AimCF:Lerp(offset, gunConfig.aimSmoothness)
	else
		local offset = CFrame.new(0,-1.2, 0)
		AimCF = AimCF:Lerp(offset, .1)
	end
end

RunService.RenderStepped:Connect(function()
	if player.Character.Humanoid.Health ~= 0 or  player.Character.Humanoid.Health ~= nil then
		
		ViewModelSway(ViewModel[EquippedGunName], ViewModel, require(ViewModel[EquippedGunName].Config))
		ViewModel:PivotTo(Camera.CFrame * ViewModelSway() * AimCF)
		Aim()
	else
		game.Workspace.Camera.ViewModel:Destroy()
	end
end)

if player.Character.Humanoid.Health ~= nil then
	EquipGun(EquippedGunName, tostring(EquippedViewModelName))
end


function getEquippedGun(gunName)
	for i, v in pairs(Inventory) do
		local VMname = tostring(EquippedViewModelName)
		for i,v in pairs(Camera:FindFirstChild(VMname):GetChildren()) do
			if v:IsA("Model") and v.Name ~= "L" and v.Name ~= "R"  and gunName ~= EquippedGunName then
				v:Destroy()
				EquippedGunName = gunName
				EquipGun(gunName, VMname)
			end
		end
	end
end

UserInputService.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean)
	if input.KeyCode == Enum.KeyCode.One then
		getEquippedGun(Inventory.Primary.Name)
	end

	if input.KeyCode == Enum.KeyCode.Two then
		getEquippedGun(Inventory.Secondary.Name)
	end

	if input.KeyCode == Enum.KeyCode.Three then
		getEquippedGun(Inventory.Melee.Name)
	end


	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		isAiming = true
	end

end)

UserInputService.InputEnded:Connect(function(input: InputObject, gameProcessedEvent: boolean) 

	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		isAiming = false
	end

end)

MODULE SCRIPT THAT I USE FOR THE LERP FUNCTION:

local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Guns = ReplicatedStorage.Guns
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local Camera = game.Workspace.CurrentCamera

local module = {}

local swayCFrame = CFrame.new()
local lastCameraCFrame = CFrame.new()

function module(gun: Model, ViewModel, config)
	local swaySize = .2
	local rot = Camera.CFrame:ToObjectSpace(lastCameraCFrame)
	local X, Y, Z = rot:ToOrientation()
	
	swayCFrame = swayCFrame:Lerp(CFrame.Angles(math.sin(X) * swaySize, math.sin(Y) * swaySize, 0), .1)

	lastCameraCFrame = Camera.CFrame

	return swayCFrame
end

return module

GUN CONFIG ( MODULE SCRIPT):

return 
	{
		Offset = CFrame.new(0, -.2, -0.4);
		canAim = true;
		aimSmoothness = .15;
		swaySize = .2;
}

I dont know like is it because the runservice ?

did you fix it? thirtycharacters

Nope I couldnt, still it gives the error of config being nil but it does print the config I dont understand