Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

[Resolved] OOP Functions Not Inherited?

Asked by
SWX253 2
3 years ago
Edited 3 years ago

I'm currently writing some OOP code for doors. For some reason, the newly created door objects return nil when they call a function of the LevelDoor class. The functions and constructor are both stored within a module script whilst a function is being called from a serverscript. I've tried calling another function and nil is returned all the same. It appears the functions aren’t being inherited. How can this be resolved and what’s causing the issue?

Error

ServerScriptService.ServerModules.LevelDoorLib Test:16: attempt to call a nil value

Module Script

local LevelDoor = {}
LevelDoor._index = LevelDoor

-- Services 
local Players = game:GetService("Players")

-- Main
function LevelDoor.new(door)
    local NewLevelDoor = {}
    --[ Properties
    NewLevelDoor.Door = door.Head
    NewLevelDoor.Config = require(door.LevelDoorConfig)
    NewLevelDoor.AreaName = NewLevelDoor.Config.AreaName or "Realm"
    NewLevelDoor.RequiredLevel = NewLevelDoor.Config.RequiredLevel or 0
    NewLevelDoor.GUI = door.BillboardGui.TextLabel

    NewLevelDoor.TeleportPositionOnAccessDenied = NewLevelDoor.Config.TeleportPositionOnAccessDenied or workspace:WaitForChild("ToSpawn").CFrame + Vector3.new(0, 2.75, 0)
    NewLevelDoor.OpenedTransparency = NewLevelDoor.Config.OpenedTransparency or 0.3
    NewLevelDoor.ClosedTransparency = NewLevelDoor.Config.ClosedTransparency or 0.6
    NewLevelDoor.TimeOpen = NewLevelDoor.Config.TimeOpen or 0.5

    --[ States
    NewLevelDoor.Cooldown = false
    NewLevelDoor.GUI.Text = NewLevelDoor.AreaName .." | Level ".. NewLevelDoor.RequiredLevel
    return setmetatable(NewLevelDoor, LevelDoor)
end

function LevelDoor:OnTouch(hit)
    local PassPlayerCheck, Player = self:IsAPlayer(hit)
    print(PassPlayerCheck, Player)
    if PassPlayerCheck and not self.Cooldown then
        self.Cooldown = true
        local PassLevelCheck = self:PassesRequiredLevel(Player)
        print(PassLevelCheck)
        if PassLevelCheck then
            self:Use()
        elseif not PassLevelCheck then
            local Character = Player.Character or Player.CharacterAdded:Wait()
            Character.PrimaryPart.CFrame = self.TeleportPositionOnAccessDenied
        end
        self.Cooldown = false
    end
end

return LevelDoor

Serverscript

-- Services
local ServerScriptService = game:GetService("ServerScriptService")

-- Modules
local LevelDoorLib = require(ServerScriptService.ServerModules.LevelDoorLib)

-- Declarations
local NewLevelDoor = LevelDoorLib.new(workspace.LevelDoorHolder.LevelDoor2)
local LevelDoor2 = workspace.LevelDoorHolder.LevelDoor2.Head

-- Main
print(LevelDoorLib)
print(NewLevelDoor)

LevelDoor2.Touched:Connect(function(hit)
    NewLevelDoor:OnTouch(hit)
end)
0
mind showing parts of the explorer? or NGC4637 602 — 3y
0
Sure SWX253 2 — 3y

1 answer

Log in to vote
0
Answered by
SWX253 2
3 years ago

The reason the functions weren’t inherited was I mistyped "__index” as ”_index”.

Ad

Answer this question