Hi,
I keep getting this error even though self.character exists, I am trying to hide the accessorys for first person. Here’s the context:
Error:
18:00:25.820 ReplicatedStorage.Source.Classes.Camera:47: attempt to index nil with 'LocalTransparencyModifier' - Client - Camera:47
I’m using strict mode btw.
function updateCharacter(character: Model)
print(character.Name) -- works fine
for _, v in pairs(character:GetChildren())do
if v:IsA("BasePart") and v.Name ~= "Head" then
v:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
v.LocalTransparencyModifier = v.Transparency
end)
v.LocalTransparencyModifier = v.Transparency
end
if v:IsA('Accessory') then
local Handle = v:FindFirstChild('Handle') :: BasePart
Handle.LocalTransparencyModifier = 1
Handle.CanCollide = false
end
if v:IsA('Hat') then
local Handle = v:FindFirstChild('Handle') :: BasePart
Handle.LocalTransparencyModifier = 1
Handle.CanCollide = false
end
end
end
full code:
--!strict
--/ services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local Workspace = game:GetService("Workspace")
--/ directories
local Dependencies = ReplicatedStorage.Source.Dependencies
local Classes = ReplicatedStorage.Source.Classes
local Packages = ReplicatedStorage.Packages
--/ requires
local Trove = require(Packages._Index["sleitnick_trove@1.1.0"]["trove"])
local Settings = require(ReplicatedStorage.Settings)
--/ variables
local CurrentCamera = workspace.CurrentCamera
local LocalPlayer = Players.LocalPlayer
local Mouse = LocalPlayer:GetMouse()
local camPos, targetPos = CurrentCamera.CFrame.Position, CurrentCamera.CFrame.Position
local angleX,targetAngleX, angleY, targetAngleY = 0,0,0,0
--/ functions
local function lerp(a: number, b: number, t: number)
return a + (b - a) * t
end
function updateCharacter(character: Model)
print(character.Name)
for _, v in pairs(character:GetChildren())do
if v:IsA("BasePart") and v.Name ~= "Head" then
v:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
v.LocalTransparencyModifier = v.Transparency
end)
v.LocalTransparencyModifier = v.Transparency
end
if v:IsA('Accessory') then
local Handle = v:FindFirstChild('Handle') :: BasePart
Handle.LocalTransparencyModifier = 1
Handle.CanCollide = false
end
if v:IsA('Hat') then
local Handle = v:FindFirstChild('Handle') :: BasePart
Handle.LocalTransparencyModifier = 1
Handle.CanCollide = false
end
end
end
--/ class
local Camera = {}
Camera.__index = Camera
export type ClassType = typeof( setmetatable({} :: {
connections: Trove.ClassType,
isRunning: boolean,
character: Model?,
}, Camera) )
function Camera.new(): ClassType
local self = {
connections= Trove.new(),
isRunning = false,
character = nil,
};
setmetatable(self, Camera)
self:_init()
return self
end
--/ private methods
function Camera._init(self: ClassType): () -- initilize
self:_setupCharacterAddedFunction()
if self.character then
updateCharacter(self.character)
end
self.connections:Connect(UserInputService.InputChanged, function(input: InputObject, gameProcessedEvent: boolean)
if input.UserInputType == Enum.UserInputType.MouseMovement then
local delta = Vector2.new(
input.Delta.X / Settings.Sensitivity,
input.Delta.Y / Settings.Sensitivity)
* Settings.Smoothness
local X = targetAngleX - delta.y
targetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X
targetAngleY = (targetAngleY - delta.x) % 360
end
end)
end
function Camera._setupCharacterAddedFunction(self: ClassType)
self.connections:Connect(Players.LocalPlayer.CharacterAdded, function(character: Model)
if not self.character then
self.character = character
end
end)
if Players.LocalPlayer then
if not self.character then
self.character = Players.LocalPlayer.Character :: Model
end
end
end
--/ update the camera (runservice)
function Camera.Update(self: ClassType, deltaTime): ()
if self.character then
if self.isRunning then
updateCharacter(self.character)
camPos += (targetPos - camPos) * 0.28
angleX += (targetAngleX - angleX) * 0.35
local head = self.character:FindFirstChild("Head") :: BasePart
local root = self.character:FindFirstChild("HumanoidRootPart") :: BasePart
local distance = targetAngleY - angleY
distance = math.abs(distance) > 180
and distance - (distance / math.abs(distance)) * 360 or distance
angleY = (angleY + distance * 0.35) % 360
CurrentCamera.CameraType = Enum.CameraType.Scriptable
CurrentCamera.CFrame = CFrame.new(head.Position)
* CFrame.Angles(0,math.rad(angleY),0)
* CFrame.Angles(math.rad(angleX),0,0)
* Settings.Offset
root.CFrame = CFrame.new(root.Position) *
CFrame.Angles(0,math.rad(angleY),0)
end
if not self.isRunning then
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
end
end
if (CurrentCamera.Focus.Position - CurrentCamera.CFrame.Position).Magnitude < 1 then
self.isRunning = false
else
self.isRunning = true
end
end
function Camera.Enable(self: ClassType): ()
local function _update(deltaTime)
self:Update(deltaTime)
end
self.connections:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value + 1, _update)
end
function Camera.Disable(self: ClassType): ()
self.connections:Clean()
end
return Camera