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

Animation on step in TFS 1.5 OTC Redemption

eardrums

Member
Joined
May 27, 2010
Messages
101
Solutions
1
Reaction score
11
Hello,
I'm trying to make a script that creates an animation that loops on step in and then stops on step out but I can't do get the event to stop on step out. it keeps looping even when i step off the tile. Anyone know what the issue is?
LUA:
local animationTimers = {} -- Table to store animation timers

-- Function to start the animation loop
function startAnimation(position)
    -- Function to send the magic effect and reschedule itself
    local function animate()
        position:sendMagicEffect(244) -- Effect to indicate tile animation
        -- Schedule the next animation
        animationTimers[position] = addEvent(animate, 2000) -- Execute every 2 seconds
    end
    -- Start the animation loop
    animate()
end

-- Function to stop the animation loop
function stopAnimation(position)
    if animationTimers[position] then
        stopEvent(animationTimers[position]) -- Stop the animation event
        animationTimers[position] = nil -- Remove the reference to the event
    end
end

-- Function triggered when a player steps on the tile
function onStepIn(creature, item, position, fromPosition)
    if item:getActionId() == 171 then
        local newTilePosition = Position(418, 1707, 7)
        local newTileId = 31115
        local newTileActionId = 172

        -- Create the new tile with ID 31115 at the specified position
        local newTile = Game.createItem(newTileId, 1, newTilePosition)
        if newTile then
            newTile:setActionId(newTileActionId)
        end
        
        -- Start looping animation
        startAnimation(newTilePosition)
    end
    return true
end

-- Function triggered when a player steps off the tile
function onStepOut(creature, item, position, fromPosition)
    if item:getActionId() == 171 then
        local tilePosition = Position(418, 1707, 7)
        local oldTileId = 31115
        local newTileId = 31114
        local actionIdToKeep = 172

        -- Stop looping animation
        stopAnimation(tilePosition)
        
        -- Check if the tile with ID 31115 exists at the specified position and has the correct action ID
        local tile = Tile(tilePosition):getItemById(oldTileId)
        if tile and tile:getActionId() == actionIdToKeep then
            -- Remove the tile with ID 31115 and create a new tile with ID 31114 but keep the action ID
            tile:remove()
            local newTile = Game.createItem(newTileId, 1, tilePosition)
            if newTile then
                newTile:setActionId(actionIdToKeep)
            end
        end
    end
    return true
end
 
So I've worked on the script a bit but sadly still have not solved the issue. no matter what i do the event does not stop on step off. It doesn't stack anymore atleast but the point for it is to stop when the player steps off. here is where i am right now.
LUA:
local teleportPosition = Position(418, 1709, 7) -- Adjust the teleport position as needed
local tilePosition = Position(418, 1707, 7)
local newTileId = 31115
local revertTileId = 31114
local newTileActionId = 172
local animationEffect = 244 -- Animation effect to use
local animationEventId = {} -- Store named event IDs

local function triggerAnimation(playerId)
    local player = Player(playerId)
    if player then
        tilePosition:sendMagicEffect(animationEffect)
        -- Schedule the next event
        animationEventId[playerId] = addEvent(triggerAnimation, 2000, playerId)
    end
end

function onStepIn(creature, item, position, fromPosition)
    if item:getActionId() == 171 then
        local player = creature:getPlayer()
        if player then
            if player:getStorageValue(30067) == -1 then
                player:teleportTo(teleportPosition)
                player:sendTextMessage(MESSAGE_STATUS_WARNING, "You have not unlocked this portal yet.")
                return true
            elseif player:getStorageValue(30067) >= 1 then
                -- Remove any existing tile with revertTileId
                local existingTile = Tile(tilePosition):getItemById(revertTileId)
                if existingTile then
                    existingTile:remove()
                end
                
                -- Create the new tile if it does not exist
                local existingNewTile = Tile(tilePosition):getItemById(newTileId)
                if not existingNewTile then
                    local newTile = Game.createItem(newTileId, 1, tilePosition)
                    if newTile then
                        newTile:setActionId(newTileActionId)
                        tilePosition:sendMagicEffect(animationEffect) -- Effect to indicate tile creation
                    end
                end

                -- Start the animation effect loop if not already running
                if not animationEventId[player:getId()] then
                    animationEventId[player:getId()] = addEvent(triggerAnimation, 2000, player:getId())
                end
            end
        end
    end
    return true
end

function onStepOut(creature, item, position, fromPosition)
    if item:getActionId() == 171 then
        local player = creature:getPlayer()
        if player then
            -- Stop the animation effect loop if it is running
            local currentEventId = animationEventId[player:getId()]
            if currentEventId then
                stopEvent(currentEventId)
                animationEventId[player:getId()] = nil
            end

            -- Revert the tile if necessary
            local tile = Tile(tilePosition):getItemById(newTileId)
            if tile and tile:getActionId() == newTileActionId then
                tile:remove()
                local revertTile = Game.createItem(revertTileId, 1, tilePosition)
                if revertTile then
                    revertTile:setActionId(newTileActionId) -- Set action ID to match
                    tilePosition:sendMagicEffect(animationEffect) -- Effect to indicate tile change
                end
            end
        end
    end
    return true
end
 
I suggest to just make the animation loop, and then add the other parts later.

try this.

LUA:
local animationInterval = 2000 -- milliseconds
local animationEffect = 244

local animationTrigger = 0

local playAnimation(position, lastKnownAnimationTrigger, interval, effect)
	-- check if the animation trigger has been reset/stopped
	if animationTrigger ~= lastKnownAnimationTrigger then
		return
	end
	-- update animation trigger
	lastKnownAnimationTrigger = lastKnownAnimationTrigger + 1
	animationTrigger = lastKnownAnimationTrigger
	-- send effect, and loop
	position:sendMagicEffect(effect)
	addEvent(playAnimation, interval, position, lastKnownAnimationTrigger, interval, effect)
end

local stopAnimation()
	animationTrigger = 0
end


function onStepIn(creature, item, position, fromPosition)
    animationTrigger = math.random(1, 1000) -- could just 'stopAnimation()', but this will get rid of edge cases
    playAnimation(position, animationTrigger, animationInterval, animationEffect)
    return true
end

function onStepOut(creature, item, position, fromPosition)
    stopAnimation()
    return true
end
 
Last edited by a moderator:
thanks. I tried this but sadly it still doesn't work when i step off the tile. the animation just continues.
LUA:
local animationInterval = 2000 -- milliseconds
local animationEffect = 244
local animationTrigger = 0
local animationPosition = Position(418, 1707, 7) -- Adjust this position as needed

local function playAnimation(position, lastKnownAnimationTrigger, interval, effect)
    -- Check if the animation trigger has been reset/stopped
    if animationTrigger ~= lastKnownAnimationTrigger then
        return
    end
    -- Update animation trigger
    lastKnownAnimationTrigger = lastKnownAnimationTrigger + 1
    animationTrigger = lastKnownAnimationTrigger
    -- Send effect and loop
    position:sendMagicEffect(effect)
    addEvent(playAnimation, interval, position, lastKnownAnimationTrigger, interval, effect)
end

local function stopAnimation()
    animationTrigger = 0
end

function onStepIn(creature, item, position, fromPosition)
    if item:getActionId() == 171 then
        -- Start the animation effect loop
        animationTrigger = math.random(1, 1000) -- Initialize animation trigger
        playAnimation(animationPosition, animationTrigger, animationInterval, animationEffect)
    end
    return true
end

function onStepOut(creature, item, position, fromPosition)
    if item:getActionId() == 171 then
        -- Stop the animation effect loop
        stopAnimation()
    end
    return true
end
 

Similar threads

Replies
7
Views
327
Xikini
X
Back
Top