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

Is this do able?

leeebux

Awsome
Premium User
Joined
May 18, 2009
Messages
104
Reaction score
5
Location
Sweden
So, i want a script that teleport a player from one position to another position after around 10sec, then again after 10sec, and once again, setting the new home to xx xx location.

The effect on the last one should also make the player go in to "Swimming"
and some effect appearing with text on the screen "Bla bla bla" (the orange text) after each teleport

No teleport effect should been seen

Using TFS 1.3

Is there anyone that could do this? or is there and similar code already out there?
 
Last edited:
So, i want a script that teleport a player from one position to another position after around 10sec, then again after 10sec, and once again, setting the new home to xx xx location.

The effect on the last one should also make the player go in to "Swimming"
and some effect appearing with text on the screen "Bla bla bla" (the orange text) after each teleport

No teleport effect should been seen

Using TFS 1.3

Is there anyone that could do this? or is there and similar code already out there?
Use support section for questions and support/requests for threads like this. This section is for releases.
 
Moved to support. Although this might fit requests more, depends if you want us to help you built this, or if you want us to code it for you.

I think what you describe can be made in lots of different ways. If you want this as a passive effect somehow, using creaturescripts/login and think events.

If you want this to trigger or work on certain locations, a combination of movements/action events with an addevent delayed effect could be used.

And yes, it is very "do able" to do this. :p
 
Moved to support. Although this might fit requests more, depends if you want us to help you built this, or if you want us to code it for you.

I think what you describe can be made in lots of different ways. If you want this as a passive effect somehow, using creaturescripts/login and think events.

If you want this to trigger or work on certain locations, a combination of movements/action events with an addevent delayed effect could be used.

And yes, it is very "do able" to do this. :p

I would like this a as a "Intro to my OT" before u start playing. :)
And is the anyone willining to do this?
 
I would like this a as a "Intro to my OT" before u start playing. :)
And is the anyone willining to do this?
Lua:
local locations = {
    [1] = {
        pos = Position(1000, 1000, 7),
        msg = "Insert text you want to display for this location here."
    },
    [2] = {
        pos = Position(1000, 1000, 7),
        msg = "Insert text you want to display for this location here."
    },
    [3] = {
        pos = Position(1000, 1000, 7),
        msg = "Insert text you want to display for this location here."
    },
}

local config = {
    town_id = 1, -- town id to be set at the end
    location_duration = 10, -- time spent at location in seconds
    storage = 60000 -- storage to set when completed
}

function onLogin(player)
    if player:getStorageValue(config.storage) > 0 then
        return true
    end

    for i = 1, #locations do
        addEvent(function(cid)
            local player = Player(cid)
            if player then
                player:teleportTo(locations[i].pos)
                player:say(locations[i].msg, TALKTYPE_MONSTER_SAY, true)
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, locations[i].msg)
                if i == #locations then
                    player:setTown(Town(config.town_id))
                    player:setStorageValue(config.storage, 1)
                end
            end
        end, i == 1 and 1000 or (i - 1) * config.location_duration * 1000, player.uid)
    end
    return true
end
 
Last edited:
Lua:
local locations = {
    [1] = {
        pos = Position(1000, 1000, 7),
        msg = "Insert text you want to display for this location here."
    },
    [2] = {
        pos = Position(1000, 1000, 7),
        msg = "Insert text you want to display for this location here."
    },
    [3] = {
        pos = Position(1000, 1000, 7),
        msg = "Insert text you want to display for this location here."
    },
}

local config = {
    town_id = 1, -- town id to be set at the end
    location_duration = 10, -- time spent at location in seconds
    storage = 60000 -- storage to set when completed
}

function onLogin(player)
    if player:getStorageValue(config.storage) > 0 then
        return true
    end

    for i = 1, #locations do
        addEvent(function()
            local player = Player(player.uid)
            if player then
                player:teleportTo(locations[i].pos)
                player:say(locations[i].msg, TALKTYPE_MONSTER_SAY, true)
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, locations[i].msg)
                if i == #locations then
                    player:setTown(Town(config.town_id))
                    player:setStorageValue(config.storage, 1)
                end
            end
        end, i == 1 and 1000 or (i - 1) * config.location_duration * 1000)
    end
    return true
end
local player = Player(player.uid)
this part is wrong, you cant use the object like that, it can be destroyed, because the function is executed later. Pass it as parameter to function
function(cid) ... i == 1 and 1000 or (i - 1) * config.location_duration * 1000, player.uid)
 
local player = Player(player.uid)
this part is wrong, you cant use the object like that, it can be destroyed, because the function is executed later. Pass it as parameter to function
function(cid) ... i == 1 and 1000 or (i - 1) * config.location_duration * 1000, player.uid)
I just tested this and it works when logged out:
Lua:
function onLogin(player)
    addEvent(function()
        local player = Player(player.uid)
        print(player)
        if player then
            print('player is here')
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'player is here')
        end
    end, 10 * 1000)
    return true
end

35227
 
I just tested this and it works when logged out:
Lua:
function onLogin(player)
    addEvent(function()
        local player = Player(player.uid)
        print(player)
        if player then
            print('player is here')
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'player is here')
        end
    end, 10 * 1000)
    return true
end

View attachment 35227
The real question is which player are you calling with print? Global or the local one because that should throw an error. If player (global one) isn't online then it wouldn't have a property of uid.

It's not a good idea to name local variables with the same name as global ones because it makes it more difficult to find the bugs in the code.
 
I just tested this and it works when logged out:
Lua:
function onLogin(player)
    addEvent(function()
        local player = Player(player.uid)
        print(player)
        if player then
            print('player is here')
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'player is here')
        end
    end, 10 * 1000)
    return true
end

View attachment 35227
Pretty sure addEvent used this way will crash the server in some situations.
 
The real question is which player are you calling with print? Global or the local one because that should throw an error. If player (global one) isn't online then it wouldn't have a property of uid.

It's not a good idea to name local variables with the same name as global ones because it makes it more difficult to find the bugs in the code.
I disagree. At this point you seem to be arguing against personal preference. Local variables will always take precedence over global if they are named similar. I see no problem with using the same variable name as I no longer have a need to reference the previous entity and essentially it's referring to the exact same thing.
Lua:
player = 1

function test(player)
    local player = 3
    print(player)
end

test(2)

35229

If anything, I can see what Nekiro is saying for safety measures to pass a local variable of cid instead, and I don't disagree with that. Just wanted to point out that it didn't cause any errors. I'll edit the original post to include this instead.

Pretty sure addEvent used this way will crash the server in some situations.
I've known that trying to pass userdata as a parameter in addEvent would flag a warning and potentially cause errors. I had always figured that the userdata was not destroyed when the player is logged out and I could still pull the unique id from it. I think my testings verify that. I'm curious as to when it would cause errors, so I can learn why this is not correct.
 

Attachments

Last edited:
I disagree. At this point you seem to be arguing against personal preference. Local variables will always take precedence over global if they are named similar. I see no problem with using the same variable name as I no longer have a need to reference the previous entity and essentially it's referring to the exact same thing.
It isn't about personal preference it's about developing poor programming habits. After all not everyone is just copying and pasting your code, some are taking it as if it is the correct way to do things & there is nothing wrong with someone critiquing your code that is how you become a better programmer whether you are a professional or hobbyist.

And this is not a true statement but I'll let you figure that out on your own ;)
Code:
Local variables will always take precedence over global
 
How to do it safe

Code:
function onLogin(player)
local name = player:getName()
    addEvent(function()
        local player = Player(name)
        print(player)
        if player then
            print('player is here')
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'player is here')
        end
    end, 10 * 1000)
    return true
end
 
It isn't about personal preference it's about developing poor programming habits. After all not everyone is just copying and pasting your code, some are taking it as if it is the correct way to do things & there is nothing wrong with someone critiquing your code that is how you become a better programmer whether you are a professional or hobbyist.

And this is not a true statement but I'll let you figure that out on your own ;)
Code:
Local variables will always take precedence over global

How to do it safe

Code:
function onLogin(player)
local name = player:getName()
    addEvent(function()
        local player = Player(name)
        print(player)
        if player then
            print('player is here')
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'player is here')
        end
    end, 10 * 1000)
    return true
end
This wouldn't work in this case because if the player logged out and logged back in before the last addEvent was triggered it would find him and send him to the last position, which wouldn't make sense. Better to make it so if they log before all events are completed then they wont get the storage set and they will have to restart the tutorial. Id is best way because it will stop all events if the player logs out.
 
Last edited by a moderator:
Using player.uid is technically safe because once the player goes offline it doesn't matter since you're simply accessing a property of a table.
Though it's technically safe, you still shouldn't be doing it that way because it could cause confusion (which it already has). Instead you should be passing player.uid as a parameter to the callback like this:
Lua:
function onLogin(player)
    addEvent(function(cid)
        local player = Player(cid)
        if player then
            -- stuff
        end
    end, 1000, player.uid)
    return true
end
This way we know the lifetime of the player exists once we use player.uid, and once it's saved we then know that we're using a saved creature id to potentially reconstruct the player rather than causing confusion by using player.uid in both the parent function and the callback.

And this is not a true statement but I'll let you figure that out on your own ;)
Yes, it is a true statement. The innermost scope can access the outer scopes, but the innermost scope takes precedence when referencing a name conflicting variable (called lexical scoping).
 
In addition to provided examples, a script that attaches to the logout creatureevent might be beneficial as well to de-initialize storage values and events. Especially if you pass events by character name. In which case the event probably shouldn't be an anonymous function.

Since uids increment over the span of the game session, passing it should be safe if you reinitialize a player object and do nothing if nil is returned.
 
So i finally compiled my OT and as a newb i dont know how to implement this code to the OT

Edit: Nvm, figured it out and its works great
 
Last edited:
Back
Top