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

AddEvent to send spectator msg

OperatorMopa

Member
Joined
Feb 10, 2014
Messages
127
Reaction score
6
I have this code
Code:
local pos = Position(354, 123, 7)

local function showMsg(playerId, position)
    local player = Player(playerId)
    if player == nil then
        print("player logged out")
        return true
    end
    player:say('hello', TALKTYPE_MONSTER_SAY)  -- it works
    local spectators = Game.getSpectators(position, false, true, 3, 3) -- it doesn't work
        for i = 1, #spectators do
        player:say('hello', TALKTYPE_MONSTER_SAY, isInGhostMode, spectators[i], position)
    end
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
   
    addEvent(showMsg, 2000, player:getId(), pos)

end

What i wanna do is to send msg to all spectators of this position. How should i do it ?
 
Code:
local function showMsg(position)
    for _, spectator in ipairs(Game.getSpectators(position, false, true, 3, 3)) do
        spectator:say("Hello World.", TALKTYPE_MONSTER_SAY)
    end
end
 
What i need is like Game.sendAnimatedText on the position but as TALKTYPE_MONSTER_SAY
Add this to player.lua
Code:
function Player:doAnimatedAllText(text)
    local textTable = {}
    text:gsub("[0-9a-zA-Z]+", function(str) table.insert(textTable, str) end)
    for i in ipairs(textTable) do
        addEvent(
        function(text, cid)
            local player = Player(cid)
            if player then
                player:say(text, TALKTYPE_MONSTER_SAY)
            end
        end, i * 1000, textTable[i], self:getId()
        )
    end
end


to use with @Printer's example
Code:
local function showMsg(position)
    for _, spectator in ipairs(Game.getSpectators(position, false, true, 3, 3)) do
        spectator:doAnimatedAllText("Hello World.")
    end
end
 
Add this to player.lua
Code:
function Player:doAnimatedAllText(text)
    local textTable = {}
    text:gsub("[0-9a-zA-Z]+", function(str) table.insert(textTable, str) end)
    for i in ipairs(textTable) do
        addEvent(
        function(text, cid)
            local player = Player(cid)
            if player then
                player:say(text, TALKTYPE_MONSTER_SAY)
            end
        end, i * 1000, textTable[i], self:getId()
        )
    end
end


to use with @Printer's example
Code:
local function showMsg(position)
    for _, spectator in ipairs(Game.getSpectators(position, false, true, 3, 3)) do
        spectator:doAnimatedAllText("Hello World.")
    end
end
It's sending above player, not position and space (" ") is making new line
 
It's sending above player, not position and space (" ") is making new line
Yes the function breaks up the words so they display properly, this function was originally made for a 8.6 server where there is a limit to how many characters (i think it was 11) can be used in the animated text function.

If you are looking for animated text at a position, sorry I don't know how to do that :(

Maybe someone else can help you. :)
 
Last edited:
I have this code
Code:
local pos = Position(354, 123, 7)

local function showMsg(playerId, position)
    local player = Player(playerId)
    if player == nil then
        print("player logged out")
        return true
    end
    player:say('hello', TALKTYPE_MONSTER_SAY)  -- it works
    local spectators = Game.getSpectators(position, false, true, 3, 3) -- it doesn't work
        for i = 1, #spectators do
        player:say('hello', TALKTYPE_MONSTER_SAY, isInGhostMode, spectators[i], position)
    end
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
  
    addEvent(showMsg, 2000, player:getId(), pos)

end

What i wanna do is to send msg to all spectators of this position. How should i do it ?
Your code works. Just tested it.
Try changing isInGhostMode to false.
 
Back
Top