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

Lua doNPCTalkALot sending messages to every player in range

Glecko

Veteran OT User
Joined
Aug 21, 2007
Messages
1,054
Reaction score
270
Location
Spain
So I have this problem. I am using the function doNPCTalkALot to make NPC's say different messages with delays.
But whenever I use that function, every player in talking range gets the messages (not just the player meant to get the message) which is very confusing for players.

I have tried this function:

Code:
	function NpcHandler:cancelNPCTalk(events)
		for aux = 1, #events do
			stopEvent(events[aux].event)
		end
		events = nil
	end

	function NpcHandler:doNPCTalkALot(msgs, interval, pcid)
		if self.eventDelayedSay[pcid] then
			self:cancelNPCTalk(self.eventDelayedSay[pcid])
		end

		self.eventDelayedSay[pcid] = {}
		local ret = {}
		for aux = 1, #msgs do
			self.eventDelayedSay[pcid][aux] = {}
			doCreatureSayWithDelay(getNpcCid(), msgs[aux], TALKTYPE_PRIVATE_NP, ((aux-1) * (interval or 10000)) + 1000, self.eventDelayedSay[pcid][aux], pcid)
			table.insert(ret, self.eventDelayedSay[pcid][aux])
		end
		return(ret)
	end

This one too:
Code:
	function npcDelayedTalk(cid, text, player)
		if isNpc(cid) and isPlayer(player) then
			doCreatureSay(cid, text, TALKTYPE_PRIVATE_NP, false, player, getThingPos(cid))
		end
	end


	function NpcHandler:doNPCTalkALot(msgs, interval, pcid)
		if self.eventDelayedSay[pcid] then
			self:cancelNPCTalk(self.eventDelayedSay[pcid])
		end

		self.eventDelayedSay[pcid] = {}
		local ret = {}
		for aux = 1, #msgs do
			self.eventDelayedSay[pcid][aux] = {}
			doCreatureSayWithDelay(getNpcCid(), msgs[aux], TALKTYPE_PRIVATE_NP, ((aux-1) * (interval or 10000)) + 1000, self.eventDelayedSay[pcid][aux], pcid)
			table.insert(ret, self.eventDelayedSay[pcid][aux])
		end
		return(ret)
	end

Those 2 have the same problem but work. Finally I tried this function:
Code:
    function storyTalk(messages, focus, i, self)
        if self:isFocused(focus) then
            selfSay(messages[i], focus)
        end
    end
    
    function NpcHandler:story(messages, focus, storyDelay)
        if self:isFocused(focus) then
            selfSay(messages[1], focus)
            local delay = storyDelay
            for i = 2, #messages do
                addEvent(storyTalk, storyDelay, messages, focus, i, self)
                storyDelay = storyDelay + delay
            end
        end
    end

Which gives me the follwing error:

error.jpg


So my conclusion is... if anybody knows what's the problem, I would be super happy (and give lots of rep++) if you fixed it, but if you have no idea why it's not working, but got a working npchandler.lua (along with the rest of the npcsystem files) I would also appreciate it a lot if you shared it :D

Thanks in advance!
 
If I understood your script correctly, it's only a function that checks if the NPC is focused before sending out a message.
That's not the problem I have, since npcHandler:say() and selfSay() work propperly (are only being sent to the targeted player). The problem only starts when I use the doNPCTalkALot function. :P
 
Back
Top