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

onStepIn removed more than 1 item under player

vexler222

Active Member
Joined
Apr 22, 2012
Messages
714
Solutions
15
Reaction score
46
Hi, i have problem with this script, it removing more than 1 item, when i has 3 banana skins under me, then script removed 2 of 3, if i has 100 banana skins, then script removed ~10.. why? Where is problem?


Lua:
local bs = MoveEvent()

function bs.onStepIn(creature, item, outfit, position)
        if creature:isPlayer() then
            item:remove() -- when i set here item:remove(1) is same situation, removing more than 1 if under me is more than 2 skins
            creature:say("Opps!", TALKTYPE_MONSTER_SAY)
            doAddCondition(creature, fight) -- this i got from older script for tfs 0.3.6 i think, cuz i don;t know how to set fight in tfs 1.5
            doCreatureSetNoMove(creature, true) -- same like ^^
            addEvent(function()
                doCreatureSetNoMove(creature, false)
            end, 3000)
        else
            return false
        end
    end

bs:id(2219)
bs:register()
 
Solution
Hi, i have problem with this script, it removing more than 1 item, when i has 3 banana skins under me, then script removed 2 of 3, if i has 100 banana skins, then script removed ~10.. why? Where is problem?


Lua:
local bs = MoveEvent()

function bs.onStepIn(creature, item, outfit, position)
        if creature:isPlayer() then
            item:remove() -- when i set here item:remove(1) is same situation, removing more than 1 if under me is more than 2 skins
            creature:say("Opps!", TALKTYPE_MONSTER_SAY)
            doAddCondition(creature, fight) -- this i got from older script for tfs 0.3.6 i think, cuz i don;t know how to set fight in tfs 1.5
            doCreatureSetNoMove(creature, true) -- same like ^^...
Hi, i have problem with this script, it removing more than 1 item, when i has 3 banana skins under me, then script removed 2 of 3, if i has 100 banana skins, then script removed ~10.. why? Where is problem?


Lua:
local bs = MoveEvent()

function bs.onStepIn(creature, item, outfit, position)
        if creature:isPlayer() then
            item:remove() -- when i set here item:remove(1) is same situation, removing more than 1 if under me is more than 2 skins
            creature:say("Opps!", TALKTYPE_MONSTER_SAY)
            doAddCondition(creature, fight) -- this i got from older script for tfs 0.3.6 i think, cuz i don;t know how to set fight in tfs 1.5
            doCreatureSetNoMove(creature, true) -- same like ^^
            addEvent(function()
                doCreatureSetNoMove(creature, false)
            end, 3000)
        else
            return false
        end
    end

bs:id(2219)
bs:register()

If banana skins don't 'stack', then this moveEvent will trigger separately for each bananaSkin.

If they don't stack, I have a work-around available, so that only 1 will trigger at a time.
Lua:
--[[

    item:remove([count = -1])
    creature:setMovementBlocked(state)
    creature:addCondition(condition[, force = false])

]]--

local bananaSkin = MoveEvent()
bananaSkin:type("stepin") -- added

local bananaSkinId = 2219

local inFight = Condition(CONDITION_INFIGHT)
inFight:setParameter(CONDITION_PARAM_TICKS, 3000) -- 3 seconds

local function resetMovementBlocked(creatureId) -- using a custom function for this, because we want to ensure the creature still exists after 3 seconds.
    local creature = Creature(creatureId)
    if creature then
        creature:setMovementBlocked(false)
    end
end

function bananaSkin.onStepIn(creature, item, position, fromPosition) -- changed.. idk why you had outfit
    if creature:isPlayer() then
        item:remove(1)
        creature:setMovementBlocked(true)
        addEvent(resetMovementBlocked, 3000, creature:getId()) -- we use the creatureId instead of creature because it's unsafe to send creatureData in an addEvent
        creature:addCondition(inFight)
        creature:say("Oops!", TALKTYPE_MONSTER_SAY)
    end
    -- no need for the else statement. It should return true regardless of what creature stepped on it.
    return true
end

bananaSkin:id(bananaSkinId)
bananaSkin:register()
Post automatically merged:

Here's the work-around.

Posting it because I'm 99% sure banana skins aren't stackable.

Lua:
local bananaSkin = MoveEvent()
bananaSkin:type("stepin")

local bananaSkinId = 2219
local cooldown = 250 -- milliseconds

local inFight = Condition(CONDITION_INFIGHT)
inFight:setParameter(CONDITION_PARAM_TICKS, 3000) -- 3 seconds

local function resetMovementBlocked(creatureId)
    local creature = Creature(creatureId)
    if creature then
        creature:setMovementBlocked(false)
    end
end

local bananaSkinVictims = {}

function bananaSkin.onStepIn(creature, item, position, fromPosition)
    if not creature:isPlayer() then
        return true
    end
    
    local creatureId = creature:getId()
    local currentTime = os.mtime()
    if bananaSkinVictims[creatureId] and bananaSkinVictims[creatureId] > currentTime then
        return true
    end
    bananaSkinVictims[creatureId] = currentTime + cooldown
    
    item:remove(1) 
    creature:setMovementBlocked(true)
    addEvent(resetMovementBlocked, 3000, creatureId)
    creature:addCondition(inFight)
    creature:say("Oops!", TALKTYPE_MONSTER_SAY)
    return true
end

bananaSkin:id(bananaSkinId)
bananaSkin:register()
 
Last edited:
Solution
@Xikini yep banana skin aren't stackable, and your script work good thanks :)
Btw, i question, i see you changed much in script, but tell me if i thinking wrong, you solved problem by adding exhaust?
 
@Xikini yep banana skin aren't stackable, and your script work good thanks :)
Btw, i question, i see you changed much in script, but tell me if i thinking wrong, you solved problem by adding exhaust?
Specifically for the banana skin issue, yes, that was solved with a cooldown/exhaust.

The rest of the changes were adding the missing functionality you wanted to add.
and/or converting the functions to proper tfs 1.0+
 
@vexler222
This code:
Lua:
            addEvent(function()
                doCreatureSetNoMove(creature, false)
            end, 3000)
is example of code that crash TFS 1.x server.
If given creature logouts/dies within 3 seconds, server will crash.

On 0.4 it would show some error in console. On 1.x it will crash server.

More information:
In this case it's 'Problem 2' - using local variable creature in addEvent

Xikini fixed it by passing creatureId and then restoring Creature object using Creature() function.
Pass creature ID (number), not object:
Lua:
local creatureId = creature:getId()
// (...)
addEvent(resetMovementBlocked, 3000, creatureId)
Restore object from creature ID and check if object exists:
Lua:
local function resetMovementBlocked(creatureId)
    local creature = Creature(creatureId)
    if creature then
        creature:setMovementBlocked(false)
    end
end
If creature does not exists (logged-out/died), Creature(creatureId) will return nil and code inside if won't execute.
 
Back
Top