• 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 Player.onMoveCreature

Lava Titan

Developer
Joined
Jul 25, 2009
Messages
1,571
Solutions
3
Reaction score
98
Location
Portugal
Hey there, I got this code that if player got storage he cant be pushed but if some1 trys to push a NPC it will send error to console so I tried

Code:
if not creature:isNpc() and player:getStorageValue(777777) == 1 then
        return false
    end

but I got this error:

Code:
Lua Script Error: [Event Interface]
data/events/scripts/player.lua:player@onMoveCreature
data/events/scripts/player.lua:98: attempt to call method 'isNpc' (a nil value)
stack traceback:
        [C]: in function 'isNpc'
        data/events/scripts/player.lua:98: in function <data/events/scripts/play
er.lua:96>
 
function Creature.isNpc(self) return false end
function Item.isNpc(self) return false end

although they might be same thing xD
 
Thanks, solved

Code:
if Creature:isPlayer(creature) and player:getStorageValue(777777) then
return false
end

With this code I no longer getting errors in console for pushing NPC or Monster
 
Thanks, solved

Code:
if Creature:isPlayer(creature) and player:getStorageValue(777777) then
return false
end

With this code I no longer getting errors in console for pushing NPC or Monster
Would be nice, if you post full code next time, because it was not possible to help you with code from first post [part of some script]. I had no idea what is 'player' and 'creature'.
 
Would be nice, if you post full code next time, because it was not possible to help you with code from first post [part of some script]. I had no idea what is 'player' and 'creature'.

That was the full code, the other code is default events from TFS 1.1

Code:
function Player.onMoveCreature(self, player, creature, fromPosition, toPosition)
    if Creature:isPlayer(creature) and player:getStorageValue(777777) then
    return false
    end
return true
end

xd
 
Thanks, solved

Code:
if Creature:isPlayer(creature) and player:getStorageValue(777777) then
return false
end

With this code I no longer getting errors in console for pushing NPC or Monster
You are lucky that your code works (or it does not work!). What TFS do you use? TFS 1.x from 9 or more months got that function ( https://github.com/otland/forgottenserver/blob/master/src/events.cpp#L527 ):
function Player.onMoveCreature(self, creature, fromPosition, toPosition)
not your:
function Player.onMoveCreature(self, player, creature, fromPosition, toPosition)

I don't know, if TFS push values to LUA functions in some reverse order (then you are LUCKY that your script works, but variable 'self' is ALWAYS EMPTY) or not (then your script always return true, because 'Creature:isPlayer(creature)' uses 'fromPosition' AS CREATURE!) - you can push everyone (not block pushing).
TFS push 4 values to that function. If you try to get 5 in yours, that means that first or last of variables (self or toPosition) is always 'nil'.
 
You are lucky that your code works (or it does not work!). What TFS do you use? TFS 1.x from 9 or more months got that function ( https://github.com/otland/forgottenserver/blob/master/src/events.cpp#L527 ):
function Player.onMoveCreature(self, creature, fromPosition, toPosition)
not your:
function Player.onMoveCreature(self, player, creature, fromPosition, toPosition)

I don't know, if TFS push values to LUA functions in some reverse order (then you are LUCKY that your script works, but variable 'self' is ALWAYS EMPTY) or not (then your script always return true, because 'Creature:isPlayer(creature)' uses 'fromPosition' AS CREATURE!) - you can push everyone (not block pushing).
TFS push 4 values to that function. If you try to get 5 in yours, that means that first or last of variables (self or toPosition) is always 'nil'.

You are right XD I didn't even notice that

I managed to fix using this

Code:
function Player.onMoveCreature(self, creature, fromPosition, toPosition)
    local player = Player(creature:getId())
    if player:getStorageValue(777777) >= 1 then
    return false
    end
return true
end

my TFS only supports:

Code:
-- Player:onMoveCreature(creature, fromPosition, toPosition) or Player.onMoveCreature(self, creature, fromPosition, toPosition)
 
You are right XD I didn't even notice that

I managed to fix using this

Code:
function Player.onMoveCreature(self, creature, fromPosition, toPosition)
    local player = Player(creature:getId())
    if player:getStorageValue(777777) >= 1 then
    return false
    end
return true
end

my TFS only supports:

Code:
-- Player:onMoveCreature(creature, fromPosition, toPosition) or Player.onMoveCreature(self, creature, fromPosition, toPosition)
What I found in TFS 1.x source code is that your code should show error, when you push npc/monster, because function Player(parameter) return nil when you pass monster/npc as parameter ( https://github.com/otland/forgottenserver/blob/master/src/luascript.cpp#L7445 ).
Your code should be (first check if 'player' is not nil and then try to get his storage):
Code:
function Player.onMoveCreature(self, creature, fromPosition, toPosition)
    local player = Player(creature:getId())
    if player and player:getStorageValue(777777) >= 1 then
    return false
    end
return true
end

from source:
https://github.com/otland/forgottenserver/blob/master/src/luascript.cpp#L7459
PHP:
if (getUserdataType(L, 2) != LuaData_Player) { // your parameter is not always a Player (can be Creature or Npc), then it return 'nil' to LUA
lua_pushnil(L);
return 1;
}
 
Another option if you don't want to build a player constructor:

Code:
function Player.onMoveCreature(self, creature, fromPosition, toPosition)
    if creature and creature:isPlayer() and creature:getStorageValue(777777) >= 1 then
        return false
    end

    return true
end

Red
 
Back
Top