Search code examples
luaroblox

Roblox RobaseService


I try to implement the RobaseService. But I have some issues with the RobaseService:GetRobase.

I get the error "ServerScriptService.RobaseService:31: attempt to index nil with 'AuthKey'". Which is this line of code:

if self.AuthKey==nil or self.AuthKey=="" then

I use the standard setup of code and have only added my url and token from Firebase. I can see in the RobaseService.new function that the url and token are correct, but when RobaseService:GetRobase() is called it says that 'self' is nil. Which does not make any sense since the RobaseService have already been initialized.

This is my RobaseService:

local Robase = require(script.Robase)
local HttpService = game:GetService("HttpService")

local RobaseService = { }
RobaseService.BaseUrl = nil
RobaseService.AuthKey = nil
RobaseService.__index = RobaseService

local function fixTrailingSlashes(url)
    local foundTrailingSlashes = url:match("/+$")
    url = foundTrailingSlashes and url:sub(1, url:len()-#foundTrailingSlashes) or url

    return url
end

function RobaseService.new(baseUrl, token)
    if baseUrl == nil then
        error("Bad Argument 1 baseUrl expected, got nil")
    elseif token==nil then
        error("Bad Argument 2 token expected, got nil")
    end

    local self = setmetatable({}, RobaseService)
    self.BaseUrl = fixTrailingSlashes(baseUrl)
    self.AuthKey = ".json?auth="..token
    return self
end

function RobaseService:GetRobase(name, scope)
    if self.AuthKey==nil or self.AuthKey=="" then
        error("You must instantiate RobaseService with an AuthKey to use the RobaseService API")
    elseif self.BaseUrl==nil or self.BaseUrl=="" then
        error("You must instantiate RobaseService with a BaseUrl to use the RobaseService API")
    end

    name = name and HttpService:UrlEncode(name) or ""
    scope = scope and HttpService:UrlEncode(scope) or ""
    
    local formatted = string.format("%s/%s/%s", self.BaseUrl, scope, name)
    --local formattedUrl = self.BaseUrl .. scope .. "/" .. name
    return Robase.new(formatted, self)
end

return RobaseService

And this is where I use the RobaseService:

local robaseModule = require(game:GetService("ServerScriptService").RobaseService)
local secretStore = game:GetService("DataStoreService"):GetDataStore("Secret")

local collectionItems = {}
local defaultSettings = {
    item1 = {ID = "SomeDude", Price = 1000, NumSold = 0, NumForSale = 999999, Level = 1, DateAdded = {Year = 2022, Month = 5, Day = 5, Hour = 11, Minute = 0, Second = 0}}
}

local RobaseService = robaseModule.new(
    secretStore:GetAsync("URL"),
    secretStore:GetAsync("AUTH")
)

local defaultRobase = RobaseService.GetRobase()

local success, serverSettings = defaultRobase:GetAsync("serverSettings")

local function applySettings()
    if serverSettings then
        print("serverSettings: ", serverSettings)
        collectionItems = serverSettings
    end
end


local function initializeSettings()
    if not serverSettings then
        warn("serverSettings finnes ikke... da har vi ikke kontakt med firebase")
        defaultRobase:SetAsync("serverSettings", defaultSettings, "PUT")
        task.wait(3)
        success, serverSettings = defaultRobase:GetAsync("serverSettings")
    end
    
    applySettings()
end


local function keepUpdating()
    --når jeg tester så bare loop på feks 10 sekunder
    --i prod så kan denne settes til feks hver time 60*60
    while task.wait(10) do
        print("oppdaterer")
        success, serverSettings = defaultRobase:GetAsync("serverSettings")
        if success then
            applySettings()
        else
            warn("ingen kontakt med Firebase. Får da ikke ut items for salg")
        end
    end
end


initializeSettings()
keepUpdating()

My url and token have been stored in a datastore and works fine. Anyone know why this would not work and why I get this error?


Solution

  • The issue isn't with how you've coded the service, but how you've called it.

    You defined the function as RobaseService:GetRobase() but you've called it with RobaseService.GetRobase(). Notice the difference between calling the function with a colon versus a period.

    In lua, defining the function with a colon is equivalent to defining the function like this : function RobaseService.GetRobase(self). And calling a function with a colon is semantic sugar for passing the object itself as the self to the function as the first argument. But because self is effectively an argument to the function, if you don't pass it in, it will be nil when your function tried to access it.

    So to fix your issue, just swap the period to a colon at the call-site:

    local defaultRobase = RobaseService:GetRobase()