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

Lua Possible to edit /m & /i talkaction to this?

FenX

Advanced OT User
Joined
Jul 8, 2019
Messages
360
Solutions
1
Reaction score
159
Hey,

Recently had a thought if the create item and create monster talkactions could have the option to spawn multiple items/monsters at once. As standard to spawn multiple with the command the ids/names would be seperated by a comma. Additionally for the create monster talkaction to also contain the "count" option.

E.g. "/m demon, rat, cat, dog" etc. same with "/i demon helmet, demon armor, demon legs"
E.g "/m demon (5), rat (3), cat (10), dog (2)" "/i demon helmet (2), demon armor (2), demon legs (2)"

How heavy of an edit would this be? Is it possible? I imagine these days it wouldn't cause any server issues or would it? Anyone willing to spend their time to do it? 😁

Here's the default code for both. FYI these are the revscriptsys versions. TFS 1.3.

create monster:
Lua:
local createMonster = TalkAction("/m")

function createMonster.onSay(player, words, param)
    if not player:getGroup():getAccess() or player:getAccountType() < ACCOUNT_TYPE_GOD then
        return true
    end

    if param == "" then
        player:sendCancelMessage("Command param required.")
        return false
    end

    local position = player:getPosition()
    local monster = Game.createMonster(param, position)
    if monster then
        monster:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
        position:sendMagicEffect(CONST_ME_MAGIC_RED)
    else
        player:sendCancelMessage("There is not enough room.")
        position:sendMagicEffect(CONST_ME_POFF)
    end
    return false
end

createMonster:separator(" ")
createMonster:register()

create item:
Lua:
local invalidIds = {
    1, 2, 3, 4, 5, 6, 7, 10, 11, 13, 14, 15, 19, 21, 26, 27, 28, 35, 43
}

local createItem = TalkAction("/i")

function createItem.onSay(player, words, param)
    if not player:getGroup():getAccess() or player:getAccountType() < ACCOUNT_TYPE_GOD then
        return true
    end

    local split = param:split(",")

    local itemType = ItemType(split[1])
    if itemType:getId() == 0 then
        itemType = ItemType(tonumber(split[1]))
        if not tonumber(split[1]) or itemType:getId() == 0 then
            player:sendCancelMessage("There is no item with that id or name.")
            return false
        end
    end

    if table.contains(invalidIds, itemType:getId()) then
        return false
    end

    local count = tonumber(split[2])
    if count then
        if itemType:isStackable() then
            count = math.min(10000, math.max(1, count))
        elseif not itemType:isFluidContainer() then
            count = math.min(100, math.max(1, count))
        else
            count = math.max(0, count)
        end
    else
        if not itemType:isFluidContainer() then
            count = 1
        else
            count = 0
        end
    end

    local result = player:addItem(itemType:getId(), count)
    if result then
        if not itemType:isStackable() then
            if type(result) == "table" then
                for _, item in ipairs(result) do
                    item:decay()
                end
            else
                result:decay()
            end
        end
        player:getPosition():sendMagicEffect(CONST_ME_MAGIC_GREEN)
    end
    return false
end

createItem:separator(" ")
createItem:register()

Let me know your thoughts!
 
Last edited:
Solution
This works for me:
Lua:
local createMonster = TalkAction("/createMonster")
function createMonster.onSay(player, words, param)
    if not player:getGroup():getAccess() or player:getAccountType() < ACCOUNT_TYPE_GOD then
        return true
    end
    if param == "" then
        player:sendCancelMessage("Command param required.")
        return false
    end
    local name, count = unpack(param:split(","))
    if not name then
        player:sendCancelMessage("The name of the monster does not exist.")
        return false
    end
    count = math.abs(tonumber(count) or 1)
    local playerPos = player:getPosition()
    for i = 1, count do
        if not Game.createMonster(name, playerPos, true) then
            player:sendCancelMessage(i ==...
try:
/m demon,5

Lua:
local createMonster = TalkAction("/m")

function createMonster.onSay(player, words, param)
    if not player:getGroup():getAccess() or player:getAccountType() < ACCOUNT_TYPE_GOD then
        return true
    end

    if param == "" then
        player:sendCancelMessage("Command param required.")
        return false
    end

    local split = param:split(",")
    local count = tonumber(split[2])
    if not count then
        player:sendCancelMessage("Command param required. Ex: /m demon,2")
        return false
    end
  
    local position = player:getPosition()
    for i = 1, count do 
        local monster = Game.createMonster(param, position, false, true)
        if monster then
            monster:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
            position:sendMagicEffect(CONST_ME_MAGIC_RED)
        else
            player:sendCancelMessage("There is not enough room.")
            position:sendMagicEffect(CONST_ME_POFF)
            break
        end
    end
    return false
end

createMonster:separator(" ")
createMonster:register()
 
Last edited:
try:
/m demon,5

Lua:
local createMonster = TalkAction("/m")

function createMonster.onSay(player, words, param)
    if not player:getGroup():getAccess() or player:getAccountType() < ACCOUNT_TYPE_GOD then
        return true
    end

    if param == "" then
        player:sendCancelMessage("Command param required.")
        return false
    end

    local split = param:split(",")
    local count = tonumber(split[2])
    if not count then
        player:sendCancelMessage("Command param required. Ex: /m demon,2")
        return false
    end
  
    local position = player:getPosition()
    for i = 1, count do 
        local monster = Game.createMonster(param, position)
        if monster then
            monster:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
            position:sendMagicEffect(CONST_ME_MAGIC_RED)
        else
            player:sendCancelMessage("There is not enough room.")
            position:sendMagicEffect(CONST_ME_POFF)
            break
        end
    end
    return false
end

createMonster:separator(" ")
createMonster:register()
It can be executed but it doesn't work. No errors in console. Doesn't allow to even spawn one.
 
It can be executed but it doesn't work. No errors in console. Doesn't allow to even spawn one.
Ofc IT wont spawn. You have to add false, true to create monster to force it since you cant create monsters on players.
Btw for position i would use getClosestPosition() of player
 
Ofc IT wont spawn. You have to add false, true to create monster to force it since you cant create monsters on players.
Btw for position i would use getClosestPosition() of player
Code:
getClosestPosition()
just returns a nil
 
Because it's not a thing in TFS, he suggest you to write it
Ah fairs, actually a good idea.
Post automatically merged:

Ofc IT wont spawn. You have to add false, true to create monster to force it since you cant create monsters on players.
Btw for position i would use getClosestPosition() of player
I'm a beginner. Could you guide me to which part i have to return. Also why? What's the role of it, what's the difference between return true/false? If you don't mind me learning a bit.
 
Last edited:
Ah fairs, actually a good idea.
Post automatically merged:


I'm a beginner. Could you guide me to which part i have to return. Also why? What's the role of it, what's the difference between return true/false? If you don't mind me learning a bit.

Code:
Game.createMonster(monsterName, position[, extended = false[, force = false]])

    Description: Creates a monster.
    Parameters:

        monsterName - Name of the monster to be created
        position - Where do we place it?
        extended - Extend the range? (optional, default: false)
        force - Will it be created even if it cannot stand at the position? (optional, default: false)

    Returns: The monster created. (userdata)
    Example:

    -- This should always create this monster.
    local monster = Game.createMonster("test", Position(100, 100, 7), false, true)
    if not monster then
        -- Something went wrong?
    end
 
This works for me:
Lua:
local createMonster = TalkAction("/createMonster")
function createMonster.onSay(player, words, param)
    if not player:getGroup():getAccess() or player:getAccountType() < ACCOUNT_TYPE_GOD then
        return true
    end
    if param == "" then
        player:sendCancelMessage("Command param required.")
        return false
    end
    local name, count = unpack(param:split(","))
    if not name then
        player:sendCancelMessage("The name of the monster does not exist.")
        return false
    end
    count = math.abs(tonumber(count) or 1)
    local playerPos = player:getPosition()
    for i = 1, count do
        if not Game.createMonster(name, playerPos, true) then
            player:sendCancelMessage(i == 1 and ("There is not enough room.") or string.format("Could not spawn %u monsters.", count-i+1))
            playerPos:sendMagicEffect(CONST_ME_POFF)
            break
        end
    end
    return false
end
createMonster:separator(" ")
createMonster:register()

Test
/createMonster rabbit,5
or
/createMonster rabbit
 
Solution
This works for me:
Lua:
local createMonster = TalkAction("/createMonster")
function createMonster.onSay(player, words, param)
    if not player:getGroup():getAccess() or player:getAccountType() < ACCOUNT_TYPE_GOD then
        return true
    end
    if param == "" then
        player:sendCancelMessage("Command param required.")
        return false
    end
    local name, count = unpack(param:split(","))
    if not name then
        player:sendCancelMessage("The name of the monster does not exist.")
        return false
    end
    count = math.abs(tonumber(count) or 1)
    local playerPos = player:getPosition()
    for i = 1, count do
        if not Game.createMonster(name, playerPos, true) then
            player:sendCancelMessage(i == 1 and ("There is not enough room.") or string.format("Could not spawn %u monsters.", count-i+1))
            playerPos:sendMagicEffect(CONST_ME_POFF)
            break
        end
    end
    return false
end
createMonster:separator(" ")
createMonster:register()

Test
/createMonster rabbit,5
or
/createMonster rabbit
Works great. 😁 👍

Now I'm curious whether or not it's a possibility to have a way to spawn different monsters/items with different counts at the same time like I previously mentioned. E.g. /createMonster demon 5, rabbit 6, rat 1 etc.

^ More of a question than a request. Wondering if it's doable.
 
Works great. 😁 👍

Now I'm curious whether or not it's a possibility to have a way to spawn different monsters/items with different counts at the same time like I previously mentioned. E.g. /createMonster demon 5, rabbit 6, rat 1 etc.

^ More of a question than a request. Wondering if it's doable.
It's doable.
 
Back
Top