So I’m trying to learn Object oriented programming, I’m creating a function that focuses the character on their mouse. But every time I try to use my stop function, it says “Attempt to index nil with Stop” and my ‘self’ value returns nil.
I’m using this OOP module I was instructed to use: https://github.com/rxi/classic/blob/master/classic.lua
Module
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Getting Player paths
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
-- Module folder paths
local Modules = ReplicatedStorage:WaitForChild("Modules")
local OOP = Modules:WaitForChild("OOP")
local ClassModule = OOP:WaitForChild("Class")
-- Requiring
local Class = require(ClassModule)
-- Functions
local Focus = Class:extend()
function CreateBodyMover(Parent)
local Force = Instance.new("BodyGyro",Parent)
Force.MaxTorque = Vector3.new(0,100000,0)
return Force
end
function UpdateRotationalForce(self)
repeat
self.BodyGyro.CFrame = CFrame.new(self.OriginPart.Position,Mouse.Hit.Position)
wait()
until self.Finished == true
self.BodyGyro:Destroy()
end
function Focus:new(Char)
self.OriginPart = Char:WaitForChild("HumanoidRootPart")
self.BodyGyro = CreateBodyMover(self.OriginPart)
self.Finished = false
local Update = coroutine.create(UpdateRotationalForce)
coroutine.resume(Update,self)
end
function Focus:Stop()
self.Finished = true
end
return Focus
Client
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Player paths
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
-- Module folder paths
local Modules = ReplicatedStorage:WaitForChild("Modules")
local Client = Modules:WaitForChild("Client")
local Abilities = Client:WaitForChild("Abilities")
local FocusModule = Abilities:WaitForChild("Focus")
-- Require Modules
local Focus = require(FocusModule)
function FocusOnMouse()
local Rotator = Focus:new(Character)
wait(5)
Rotator:Stop()
end
FocusOnMouse()
