• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

TFS 1.0 VIP System OnLogin

CharlesHudson

Just alive, or just breathing...
Joined
Oct 25, 2011
Messages
98
Reaction score
14
Hello Everybody
i'm looking for help to fix this error
Code:
Lua Script Error: [CreatureScript Interface]
data/creaturescripts/scripts/others/login.lua:onLogin
data/vipsystem.lua:31: attempt to index a nil value
stack traceback:
        [C]: in function '__index'
        data/vipsystem.lua:31: in function 'getVipDays'
        data/creaturescripts/scripts/others/login.lua:181: in function <data/creaturescripts/scripts/others/login.lua:84
I'm using this VIP SYSTEM.lua
Lua:
local config = {
    -- true = player will be teleported to this position if Vip runs out
    -- false = player will not be teleported
    useTeleport = true,
    expirationPosition = Position(32369, 32241, 7),

    -- true = player will received the message you set
    -- false = player will not receive a message
    useMessage = true,
    expirationMessage = 'Your vip days ran out.',
    expirationMessageType = MESSAGE_STATUS_WARNING
}

if not VipData then
    VipData = { }
end

function Player.onRemoveVip(self)
    if config.useTeleport then
        self:teleportTo(config.expirationPosition)
        config.expirationPosition:sendMagicEffect(CONST_ME_TELEPORT)
    end

    if config.useMessage then
        self:sendTextMessage(config.expirationMessageType, config.expirationMessage)
    end
end

function Player.getVipDays(self)
    return VipData[self:getId()].days
end

function Player.getLastVipDay(self)
    return VipData[self:getId()].lastDay
end

function Player.isVip(self)
    return self:getVipDays() > 0
end

function Player.addInfiniteVip(self)
    local data = VipData[self:getId()]
    data.days = 0xFFFF
    data.lastDay = 0

    db.query(string.format('UPDATE `accounts` SET `vipdays` = %i, `viplastday` = %i WHERE `id` = %i;', 0xFFFF, 0, self:getAccountId()))
end

function Player.addVipDays(self, amount)
    local data = VipData[self:getId()]
    local amount = math.min(0xFFFE - data.days, amount)
    if amount > 0 then
        if data.days == 0 then
            local time = os.time()
            db.query(string.format('UPDATE `accounts` SET `vipdays` = `vipdays` + %i, `viplastday` = %i WHERE `id` = %i;', amount, time, self:getAccountId()))
            data.lastDay = time
        else
            db.query(string.format('UPDATE `accounts` SET `vipdays` = `vipdays` + %i WHERE `id` = %i;', amount, self:getAccountId()))
        end
        data.days = data.days + amount
    end

    return true
end

function Player.removeVipDays(self, amount)
    local data = VipData[self:getId()]
    if data.days == 0xFFFF then
        return false
    end

    local amount = math.min(data.days, amount)
    if amount > 0 then
        db.query(string.format('UPDATE `accounts` SET `vipdays` = `vipdays` - %i WHERE `id` = %i;', amount, self:getAccountId()))
        data.days = data.days - amount

        if data.days == 0 then
            self:onRemoveVip()
        end
    end

    return true
end

function Player.removeVip(self)
    local data = VipData[self:getId()]
    if data.days == 0 then
        return
    end

    data.days = 0
    data.lastDay = 0

    self:onRemoveVip()

    db.query(string.format('UPDATE `accounts` SET `vipdays` = 0, `viplastday` = 0 WHERE `id` = %i;', self:getAccountId()))
end

function Player.loadVipData(self)
    local resultId = db.storeQuery(string.format('SELECT `vipdays`, `viplastday` FROM `accounts` WHERE `id` = %i;', self:getAccountId()))
    if resultId then
        VipData[self:getId()] = {
            days = result.getDataInt(resultId, 'vipdays'),
            lastDay = result.getDataInt(resultId, 'viplastday')
        }

        result.free(resultId)
        return true
    end

    VipData[self:getId()] = { days = 0, lastDay = 0 }
    return false
end

function Player.updateVipTime(self)
    local save = false

    local data = VipData[self:getId()]
    local days, lastDay = data.days, data.lastDay
    local daysBefore = days
    if days == 0 or days == 0xFFFF then
        if lastDay ~= 0 then
            lastDay = 0
            save = true
        end
    elseif lastDay == 0 then
        lastDay = os.time()
        save = true
    else
        local time = os.time()
        local elapsedDays = math.floor((time - lastDay) / 86400)
        if elapsedDays > 0 then
            if elapsedDays >= days then
                days = 0
                lastDay = 0
            else
                days = days - elapsedDays
                lastDay = time - ((time - lastDay) % 86400)
            end
            save = true
        end
    end

    if save then
        if daysBefore > 0 and days == 0 then
            self:onRemoveVip()
        end

        db.query(string.format('UPDATE `accounts` SET `vipdays` = %i, `viplastday` = %i WHERE `id` = %i;', days, lastDay, self:getAccountId()))
        data.days = days
        data.lastDay = lastDay
    end
end
Plxx need help
THANKS IN ADVANCE
Hope u guys are well
 
Solution
since he didn't post the solution and instead posted "no help", i'll post the solution like you're supposed to in case somebody else has this problem.
Lua:
function Player.getVipDays(self)
    return VipData[self:getId()].days
end
to
Lua:
function Player.getVipDays(self)
    return VipData[self:getId()] and VipData[self:getId()].days or -1
end
since he didn't post the solution and instead posted "no help", i'll post the solution like you're supposed to in case somebody else has this problem.
Lua:
function Player.getVipDays(self)
    return VipData[self:getId()].days
end
to
Lua:
function Player.getVipDays(self)
    return VipData[self:getId()] and VipData[self:getId()].days or -1
end
 
Solution
Don't works, already try it
the solution is
Lua:
    -- vipdays
        local days = player:getVipDays()
      if  days == 0 then
        player:sendTextMessage(MESSAGE_STATUS_DEFAULT,('You have no vip.'))
        elseif  days ~= 0 then
        player:sendTextMessage(MESSAGE_STATUS_DEFAULT,('You have '.. days ..' days VIP left.'))
On login.lua
 
that doesn't fix any error about indexing a nil value inside of the getVipDays function. you should also use >= 0 for the second conditional statement so you don't get the false message of negative days of vip left
 
Oh sorry, i forgot to say i add a bugged line before add that, i copy the wrong system and start to bug all, also creaturescripts have one too so, got millions of error, so i erease everything and add that, and get no error, and works so good
 
Back
Top