ModuleScript not doing what expected

Hi! I’m working on a guns system, i want to set the body transparency to 0 in first person, i know how to do it but i made the function on a ModuleScript in a folder of ReplicatedStorage and calling it from a LocalScript which control the action of the gun tool

ModuleScript:

function functions.toggleFirstPerson(player)
	local character = player.Character
	local humanoid = character:WaitForChild("Humanoid")
	
	if player.CameraMode == Enum.CameraMode.Classic then
		player.CameraMode = Enum.CameraMode.LockFirstPerson
		
		for i, v in pairs(character:GetChildren()) do
			if v:IsA("BasePart") and v.Name ~= "Head" then
				v.LocalTransparencyModifier = 0
			end
		end

		humanoid.CameraOffset = Vector3.new(0, 0, -1.5)
	else
		player.CameraMode = Enum.CameraMode.Classic
		
		humanoid.CameraOffset = Vector3.new(0, 0, 0)
	end
end

LocalScript:

local functions = require(game:GetService("ReplicatedStorage"):WaitForChild("Modules").Guns)

script.Parent.Equipped:Connect(function()

    functions.toggleFirstPerson(player)

end)

script.Parent.Unequipped:Connect(function()

    functions.toggleFirstPerson(player)

end)

i’m not receiving errors but this doesn’t work, if i put the function on the LocalScript it works. Do you know how to fix this? Thank you!

1 Like

Did you know that, when a function is using . instead of : operator, there’s a distinction whether self is involved or not?

Option A:

function functions.toggleFirstPerson(self, player)
	local character = player.Character
	local humanoid = character:WaitForChild("Humanoid")
	
	if player.CameraMode == Enum.CameraMode.Classic then
		player.CameraMode = Enum.CameraMode.LockFirstPerson
		
		for i, v in pairs(character:GetChildren()) do
			if v:IsA("BasePart") and v.Name ~= "Head" then
				v.LocalTransparencyModifier = 0
			end
		end

		humanoid.CameraOffset = Vector3.new(0, 0, -1.5)
	else
		player.CameraMode = Enum.CameraMode.Classic
		
		humanoid.CameraOffset = Vector3.new(0, 0, 0)
	end
end

Option B:

function functions:toggleFirstPerson(player)
	local character = player.Character
	local humanoid = character:WaitForChild("Humanoid")
	
	if player.CameraMode == Enum.CameraMode.Classic then
		player.CameraMode = Enum.CameraMode.LockFirstPerson
		
		for i, v in pairs(character:GetChildren()) do
			if v:IsA("BasePart") and v.Name ~= "Head" then
				v.LocalTransparencyModifier = 0
			end
		end

		humanoid.CameraOffset = Vector3.new(0, 0, -1.5)
	else
		player.CameraMode = Enum.CameraMode.Classic
		
		humanoid.CameraOffset = Vector3.new(0, 0, 0)
	end
end

There’s more information here:


I ran additionally testing, so there’s a distinction.

local safe = {}

function safe:test(a, b)
    print(self)
    print(a, b)
end

safe.test(1, 2) -- outputs 1, then 2 and nil
safe:test(1, 2) -- outputs 1 and 2
local safe = {}

function safe.test(a, b)
    print(a, b)
end

safe.test(1, 2) -- outputs 1 and 2
safe:test(1, 2) -- outputs memory address to table and 1
1 Like

Ok thanks now it is calling the function but i get this error :confused:

ReplicatedStorage.Modules.Guns:51: attempt to index nil with 'Character'

1 Like

You realize that you aren’t setting anything on player, do you?

Use Players.LocalPlayer.

1 Like

Ok, but if i would send something from the local script how should i do it?

1 Like

Assuming that you changed the module’s function to:
function functions:toggleFirstPerson(player)

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer -- optional if there's another use for it, otherwise use Players.LocalPlayer instantly

local functions = require(game:GetService("ReplicatedStorage"):WaitForChild("Modules").Guns)

script.Parent.Equipped:Connect(function()
    functions:toggleFirstPerson(LocalPlayer)
end)

script.Parent.Unequipped:Connect(function()
    functions:toggleFirstPerson(LocalPlayer)
end)
1 Like

Oh ok i added the self parameter instead of changing “.” with “:”, thank you!