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

TFS 1.X+ Doubt About How Can I Use Creature Functions in AddEvent?

Yan18

Member
Joined
Jun 14, 2014
Messages
104
Solutions
3
Reaction score
17
Hello Everyone! The first of all, Happy New Year folks!!!

My doubt as the title says, how can I use creture functions in addEvent? For example, I want to use the following function inside addEvent:

Lua:
player:getPosition():sendMagicEffect(effect)

I made a local function to return player:getPosition():sendMagicEffect to I can use that function inside addEvent:

Lua:
local effect = 25

-- LOCAL FUNCTION TO USE SENDMAGICEFFECT IN ADDEVENT--
local function returnMagic(cid, effect)
   local player = Player(cid)
   return player:getPosition():sendMagicEffect(effect)
end

function onSay(player, words, param)
    for i = 1, 5 do
         addEvent(returnMagic, 60000, player, effect)
    end   
end

When I use like below, it generates am error on console:
Lua:
Lua Script Error: [TalkAction Interface]
data/talkactions/scripts/repeat_effect.lua:onSay
LuaScriptInterface::luaAddEvent(). Argument #3 is unsafe
stack traceback:
        [C]: in function 'addEvent'
        data/talkactions/scripts/repeat_effect.lua:15: in function <data/talkactions/scripts/repeat_effect.lua:11>

And I was searching about this error and I saw that is dangerous use Userdata in addEvent parameters, but I don't know how to do to works my script.
 
Solution
Yes, I had forgot to add i in time on addEvent. It works! Thanks friend, but can you explain to me how do eternal loop? I tried with while and repeat but characterer stay freezed and crash otclient.
put addEvent inside a function too, exemple, execute every 5 seconds:


Lua:
local function returnMagic(cid, effect)
   local player = Player(cid)
   if not player then return true end
   return player:getPosition():sendMagicEffect(effect) and addEvent(returnMagic, 1000, player:getId(), effect)
end

function onSay(player, words, param)
    returnMagic(player:getId(), effect)
    return true
end
player is like poiter in c++, check this script when event will be trigered and player will be offline, 99% chance fto crash server, in addevent add this player.uid instead player, on function check player, if player exist make effect
 
Just guessing, but is "player" in the addEventCall a "Player object" (i.e. equivalent to the result of "player = Player(cid)") or is it a "cid"?
If so, are they different data types (I'm guessing "yes" but just from the information in your post)?

Compilers/Interpreters don't like mismatching data types in parameters that are being passed around. The process of dealing with this includes the concept of "Type Safety", which is consistent with the main error message.

Edit: I was late posting, but FWIW my post is a wordy version of Roriscrave's suggestion :)
 
addEvent(returnMagic, 60000, player:getId(), effect)
Thanks for suppor bro, I have already tried use player:getId() instead player and it doesn't generate an error, but nothing happen!

player is like poiter in c++, check this script when event will be trigered and player will be offline, 99% chance fto crash server, in addevent add this player.uid instead player, on function check player, if player exist make effect
I don't think the problem is if the player are online or offline, because I'm testing in talktactions. And like I answered our friend above, I have already tried use player:getId() that is the same thing player.uid to TFS 0.x and nothing happened. But thanks to try help me.

Just guessing, but is "player" in the addEventCall a "Player object" (i.e. equivalent to the result of "player = Player(cid)") or is it a "cid"?
If so, are they different data types (I'm guessing "yes" but just from the information in your post)?

Compilers/Interpreters don't like mismatching data types in parameters that are being passed around. The process of dealing with this includes the concept of "Type Safety", which is consistent with the main error message.

Edit: I was late posting, but FWIW my post is a wordy version of Roriscrave's suggestion :)
Thanks for the explanation bro, I have already tried the suggestion of our friend above (player:getId()) but nothing happen :confused:.
 
Thanks for suppor bro, I have already tried use player:getId() instead player and it doesn't generate an error, but nothing happen!


I don't think the problem is if the player are online or offline, because I'm testing in talktactions. And like I answered our friend above, I have already tried use player:getId() that is the same thing player.uid to TFS 0.x and nothing happened. But thanks to try help me.


Thanks for the explanation bro, I have already tried the suggestion of our friend above (player:getId()) but nothing happen :confused:.
Lua:
local effect = 25

-- LOCAL FUNCTION TO USE SENDMAGICEFFECT IN ADDEVENT--
local function returnMagic(cid, effect)
   local player = Player(cid)
   if not player then return true end
   return player:getPosition():sendMagicEffect(effect)
end

function onSay(player, words, param)
    for i = 1, 5 do
         addEvent(returnMagic, 60000, player:getId(), effect)
    end   
end
 
Lua:
local effect = 25

-- LOCAL FUNCTION TO USE SENDMAGICEFFECT IN ADDEVENT--
local function returnMagic(cid, effect)
   local player = Player(cid)
   if not player then return true end
   return player:getPosition():sendMagicEffect(effect)
end

function onSay(player, words, param)
    for i = 1, 5 do
         addEvent(returnMagic, 60000, player:getId(), effect)
    end  
end
It didn't work :confused:.
 
It didn't work :confused:.
error? what u need to code to? this way you are using the "for" function without needing....
if you want the effect to come out every X seconds, change that part
Lua:
addEvent(returnMagic, 60000, player:getId(), effect)
for something like this:
Lua:
addEvent(returnMagic, 1000 * i, player:getId(), effect)
 
error? what u need to code to? this way you are using the "for" function without needing....
if you want the effect to come out every X seconds, change that part
Lua:
addEvent(returnMagic, 60000, player:getId(), effect)
for something like this:
Lua:
addEvent(returnMagic, 1000 * i, player:getId(), effect)
I just used structure for to demonstrate an example, but I want to do a script that repeat an effect for specific times that I want. I was using repeat and I tried while before.
 
I just used structure for to demonstrate an example, but I want to do a script that repeat an effect for specific times that I want. I was using repeat and I tried while before.
the code I posted above is working, I tested it here on my computer.
 
the code I posted above is working, I tested it here on my computer.
Yes, I had forgot to add i in time on addEvent. It works! Thanks friend, but can you explain to me how do eternal loop? I tried with while and repeat but characterer stay freezed and crash otclient.
 
Yes, I had forgot to add i in time on addEvent. It works! Thanks friend, but can you explain to me how do eternal loop? I tried with while and repeat but characterer stay freezed and crash otclient.
put addEvent inside a function too, exemple, execute every 5 seconds:


Lua:
local function returnMagic(cid, effect)
   local player = Player(cid)
   if not player then return true end
   return player:getPosition():sendMagicEffect(effect) and addEvent(returnMagic, 1000, player:getId(), effect)
end

function onSay(player, words, param)
    returnMagic(player:getId(), effect)
    return true
end
 
Solution
put addEvent inside a function too, exemple, execute every 5 seconds:


Lua:
local function returnMagic(cid, effect)
   local player = Player(cid)
   if not player then return true end
   return player:getPosition():sendMagicEffect(effect) and addEvent(returnMagic, 1000, player:getId(), effect)
end

function onSay(player, words, param)
    returnMagic(player:getId(), effect)
    return true
end
Thanks friend! It works perfect! Thank you very much!
 
Back
Top