• 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!

CreatureEvent [TFS 1.X] Notification: Player can use X Spells on Advance

Leo32

Getting back into it...
Joined
Sep 21, 2007
Messages
987
Solutions
14
Reaction score
532
XcJPzvp.png


pR8qhvk.png


This was mainly put together due to:

This only works if 'needlearn' for spells is set to off.

We have a 0.4 version, a 1.3 version.
Mays well edit it for 1.0-1.2 and release it too.

Ignore the spells being learned in my screenshots, I've been messing with vocations and spells for the past few weeks.
They're just examples showing the appropriate grammar and formatting depending on the number of spells learned.

creaturescripts.xml add:
XML:
<event type="advance" name="SpellNotifier" script="spellnotifier.lua"/>

creaturescripts\scripts\login.lua add:
Lua:
-- Events
player:registerEvent("SpellNotifier")

creaturescripts\scripts\spellnotifier.lua:
Lua:
function onAdvance(player, skill, oldLevel, newLevel)

    if skill == SKILL_LEVEL then

        function grammarstring(spelltable)
            local s = ""
            local commacounter = 0
            local lastcomma = -1
        
            -- Put commas between spells
            for k,v in ipairs(spelltable) do
                if k ~= 1 then
                    spelltable[k] = ", " .. v
                end
                commacounter = commacounter+1
                lastcomma = k
            end
        
            -- Edit the values for grammar depending on the comma count
            if commacounter > 1 then
                spelltable[1] = "s " .. spelltable[1] -- plural, add s
                spelltable[lastcomma] = spelltable[lastcomma]:gsub(", ", " and ") -- replace last comma with an 'and'
            else
                spelltable[1] = " " .. spelltable[1] -- only 1 result, just add a space
            end
        
            -- Put final values into a single string
            for k,v in ipairs(spelltable) do
                s = s .. v
            end
        
            return s
        end
    
        local count = getPlayerInstantSpellCount(player)
        local spells = {}
        for i = 0, count - 1 do
            local spell = getPlayerInstantSpellInfo(player, i)
            if spell.level > oldLevel and spell.level <= newLevel then
                table.insert(spells, "'" .. spell.name .. "'")
            end
        end
    
        if #spells > 0 then
            player:sendTextMessage(MESSAGE_INFO_DESCR, "You have learned the spell" .. grammarstring(spells) .. ".")
        end
    end

    return true
end
 
Last edited:
Is this for 0.4?

Getting this error on 1.3 🤔
Code:
Lua Script Error: [CreatureScript Interface]
data/creaturescripts/scripts/spellnotifier.lua:onAdvance
data/creaturescripts/scripts/spellnotifier.lua:39: attempt to index local 'spell' (a boolean value)
stack traceback:
    [C]: in function '__index'
    data/creaturescripts/scripts/spellnotifier.lua:39: in function <data/creaturescripts/scripts/spellnotifier.lua:1>
 
Its 1.0-1.2
Getting spell information changed in 1.3 so you have to change:
Lua:
local count = getPlayerInstantSpellCount(player)
local spells = {}
for i = 0, count - 1 do
    local spell = getPlayerInstantSpellInfo(player, i)
    if spell.level > oldLevel and spell.level <= newLevel then
        table.insert(spells, "'" .. spell.name .. "'")
    end
end

to:
Lua:
local spells = {}
for _, spell in ipairs(player:getInstantSpells()) do
    if spell.level > oldLevel and spell.level <= newLevel then
        table.insert(spells, "'" .. spell.name .. "'")
    end
end

Here you go:
Lua:
function onAdvance(player, skill, oldLevel, newLevel)

    if skill == SKILL_LEVEL then

        function grammarstring(spelltable)
            local s = ""
            local commacounter = 0
            local lastcomma = -1
      
            -- Put commas between spells
            for k,v in ipairs(spelltable) do
                if k ~= 1 then
                    spelltable[k] = ", " .. v
                end
                commacounter = commacounter+1
                lastcomma = k
            end
      
            -- Edit the values for grammar depending on the comma count
            if commacounter > 1 then
                spelltable[1] = "s " .. spelltable[1] -- plural, add s
                spelltable[lastcomma] = spelltable[lastcomma]:gsub(", ", " and ") -- replace last comma with an 'and'
            else
                spelltable[1] = " " .. spelltable[1] -- only 1 result, just add a space
            end
      
            -- Put final values into a single string
            for k,v in ipairs(spelltable) do
                s = s .. v
            end
      
            return s
        end
  
        local spells = {}
        for _, spell in ipairs(player:getInstantSpells()) do
            if spell.level > oldLevel and spell.level <= newLevel then
                table.insert(spells, "'" .. spell.name .. "'")
            end
        end
  
        if #spells > 0 then
            player:sendTextMessage(MESSAGE_INFO_DESCR, "You have learned the spell" .. grammarstring(spells) .. ".")
        end
    end

    return true
end
 
Last edited:
Back
Top