• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Request - Monster Spell- TFS 1.3 - version 10.99

Taurus

Texass
Joined
Jan 11, 2009
Messages
616
Solutions
2
Reaction score
30
Location
United States
I would like a spell where the monster has a chance to teleport himself to one of a few locations.

The spell should only teleport the monster, I think it should be pretty easy.

Thanks to whoever can help.

TFS 1.3 (OTX)/Version 10.99
 
Last edited:
Solution
change:
LUA:
function onCastSpell(cid, var)
to:
LUA:
function onCastSpell(creature, variant)

You should move the table shuffling function somewhere to the lib folder or just add this to global.lua:

LUA:
dofile('data/lib/mystuff.lua')
then create that file and include all of your custom functions there so you can easily keep track of global functions you add
You didn't specify how the locations are determined.

LUA:
local chance = 10
local locations = {
    {x = 111, y = 111, z = 7},
    {x = 222, y = 222, z = 7},
    {x = 333, y = 333, z = 7},
}
   
if math.random(100) <= chance then
    local destination = creature:getClosestFreePosition(locations[math.random(#locations)])
    if destination.x ~= 0 then
        creature:teleportTo(destination, false)
    end
end
 
You didn't specify how the locations are determined.

LUA:
local chance = 10
local locations = {
    {x = 111, y = 111, z = 7},
    {x = 222, y = 222, z = 7},
    {x = 333, y = 333, z = 7},
}
 
if math.random(100) <= chance then
    local destination = creature:getClosestFreePosition(locations[math.random(#locations)])
    if destination.x ~= 0 then
        creature:teleportTo(destination, false)
    end
end
Wow, thank you that is great.

If I understand right this will go to closest free position(or does that mean the closest position to the destination)... can I make it ignore the closest free destination somehow?

Like if it is close to the destination it should choose another instead

I'd like to make a monster that teleports himself to a location on a platform other than the one he is on, basically so the players have to chase him before he heals too much or use multiple teams.
 
Last edited:
LUA:
if math.random(100) <= chance then
    locations = shuffleTable(locations)
       
    for i = 1, #locations do
        local tile = Tile(locations[i])
        if tile and tile:getCreatureCount() == 0 and not tile:hasProperty(CONST_PROP_IMMOVABLEBLOCKSOLID) then
            creature:teleportTo(locations[i], false)
            break
        end
    end
end

You will also need this if you don't have any table shuffling function:

LUA:
function shuffleTable(tab)
    local n, order, res = #tab, {}, {}
    for i = 1,n do
        order[i] = {rnd = math.random(), idx = i}
    end
    table.sort(order, function(a,b) return a.rnd < b.rnd end)
    for i = 1,n do
        res[i] = tab[order[i].idx]
    end
    return res
end
 
You will also need this if you don't have any table shuffling function:
Where should I put it? I placed in script for now, but it seems like I should add somewhere else.

Also, I've got this for script so far and getting console errors.

(messy)
LUA:
function shuffleTable(tab)
    local n, order, res = #tab, {}, {}
    for i = 1,n do
        order[i] = {rnd = math.random(), idx = i}
    end
    table.sort(order, function(a,b) return a.rnd < b.rnd end)
    for i = 1,n do
        res[i] = tab[order[i].idx]
    end
    return res
end

local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_NONE)
setCombatParam(combat, COMBAT_PARAM_EFFECT, CONST_ME_NONE)


local chance = 99
local locations = {
    {x = 31676, y = 32316, z = 11},
    {x = 31695, y = 32298, z = 12},
    {x = 31679, y = 32299, z = 12},
}

function onCastSpell(cid, var)

    if math.random(100) <= chance then
        locations = shuffleTable(locations)
       
        for i = 1, #locations do
            local tile = Tile(locations[i])
            if tile and tile:getCreatureCount() == 0 and not tile:hasProperty(CONST_PROP_IMMOVABLEBLOCKSOLID) then
                creature:teleportTo(locations[i], false)
                break
            end
        end
    end
end

qEIEPpz.png
 
change:
LUA:
function onCastSpell(cid, var)
to:
LUA:
function onCastSpell(creature, variant)

You should move the table shuffling function somewhere to the lib folder or just add this to global.lua:

LUA:
dofile('data/lib/mystuff.lua')
then create that file and include all of your custom functions there so you can easily keep track of global functions you add
 
Solution
change:
LUA:
function onCastSpell(cid, var)
to:
LUA:
function onCastSpell(creature, variant)

You should move the table shuffling function somewhere to the lib folder or just add this to global.lua:

LUA:
dofile('data/lib/mystuff.lua')
then create that file and include all of your custom functions there so you can easily keep track of global functions you add

Frija thank you so much. I was able to get this working. You are too awesome.

I will do exactly what you said, with the function and also, a special thank you for showing me the:
LUA:
function onCastSpell(creature, variant)

VERY good to know. Where can I learn more about this, it is a common issue I face.
 
TFS 1.2+ uses creature and variant as onCastSpell function arguments. If you are using TFS1.3 then every spell should use these be default. No idea how "function onCastSpell(cid, var)" got into you spell, maybe from some other script that was written for older TFS version?
 
Back
Top