EDIT: Fixed
Hey DevForum, I’ve been learning how to use OOP (Object Oriented Programming) whilst scripting and decided to try and put it to use by making an FPS system.
The problem I’m having is that whenever I try to reference the :Aim()
and :Unequip()
function, it returns nil for some reason.
Handler module:
local Framework = require(game.ReplicatedStorage.Modules.Client.FPS.FPSFramework)
local UIS = game:GetService('UserInputService')
local Handler = {}
Handler.CanInitialise = true
function Handler:Init()
local Player = game.Players.LocalPlayer
local CurrentEquipped
Player.Backpack.L119A2.Equipped:Connect(function()
CurrentEquipped = Framework.new('L119A2')
for i, Part in pairs (CurrentEquipped.Weapon:GetChildren()) do
print('Looping through parts')
if Part:IsA('MeshPart') or Part:IsA('Part') then
print('checking if instance is part')
Part.Transparency = 1
print(Part.Transparency)
end
end
UIS.InputBegan:Connect(function(Key)
if Key.UserInputType == Enum.UserInputType.MouseButton2 then
CurrentEquipped:Aim(true)
end
end)
end)
Player.Backpack.L119A2.Unequipped:Connect(function()
CurrentEquipped:Unequip()
end)
end
return Handler
Framework module:
local FPS = {}
FPS.__index = FPS
local RunService = game:GetService('RunService')
local TweenService = game:GetService('TweenService')
local Camera = game.Workspace.CurrentCamera
local NormalCF = CFrame.new()
local AimCF = CFrame.new()
function FPS.new(Weapon)
local self = {}
self.Config = require(game.ReplicatedStorage.Modules.Client.FPS.GunConfig)[Weapon]
self. Viewmodel = self.Config.Viewmodel:Clone()
self.Viewmodel.Parent = Camera
self.Weapon = self.Config.Tool
self.Shooting = false
self.Aiming = false
self.Reloading = false
-- Rendering framework
RunService.RenderStepped:Connect(function()
NormalCF = NormalCF:Lerp(Camera.CFrame, 0.6)
AimCF = NormalCF:Lerp((Camera.CFrame * CFrame.new(-1.2, 0.175, 3)), 0.9)
if self.Viewmodel.Parent == Camera then
-- Normal
if self.Aiming == false then
self.Viewmodel:SetPrimaryPartCFrame(NormalCF)
-- Aiming
elseif self.Aiming == true then
print('aiming')
self.Viewmodel:SetPrimaryPartCFrame(AimCF)
end
else
return false
end
end)
return setmetatable(self, FPS)
end
function FPS:Unequip()
if self.Viewmodel.Parent == Camera then
self.Viewmodel:Destroy()
end
end
function FPS:Aim(Value)
print('setting aim')
self.Aiming = Value
end
return FPS
I have a localscript which runs the handler module, don’t worry about that
As you can see, I’ve place a bunch of prints in order to see where it goes wrong. I’m sure this is an easy fix but I’ve been looking everywhere and can’t find anything that helps. I have this same problem with another OOP system I tried making whilst learning how to use OOP so if you guys have a fix it would be very appreciated, thanks.