• 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 0.X How can i improve this script?

juansanchez

Well-Known Member
Joined
Apr 2, 2015
Messages
206
Reaction score
84
Hey guys, i was wondering if anyone could help me improve this script.
I'm using TFS 0.3.7

I created this script from scratch, but i'm not very good with scripting.

One of the problems i found was: The script checks if the Oberon is still alive to remove it if someone doesn't kill it in time/dies, but if the player kills the Oberon, when the script checks for the monster i get an error saying Creature not found (obviously). And i can't think of an way to improve this.

Also, the player has to stay in the room for 6 minutes before he gets kicked, and if the players kill it fast that can be a bit annoying. But if i just make a teleport for the player to leave the room without some sort of storage, he gets all the way to the city, and the script teleports him back to the Oberon lever. I was thinking of some sort storage on the kick time or something, but i'm not really sure what to do.

Any suggestions/improvements?
And if anyone has some other suggestions not related to the ones i said, feel free to tell me so i can improve it.
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)

local from, to = {x = 1635, y = 1574, z = 8}, {x = 1538, y = 1620, z = 8}
local newPos = {x = 1643, y = 1585, z = 8}
local afterPos = {x = 1634, y = 1604, z = 8}
local kickTime = 6 * 60 * 1000 -- 6 Minutes
local cooldown = 720000
local reset = 60
local storage = 10123

    if getPlayerReset(cid) < reset then
        doSendMagicEffect(getCreaturePosition(cid), 2)
        return doPlayerSendTextMessage(cid,20, 'Voce precisa de '.. reset ..' resets ou mais.') -- Checks if the player has level to use the lever.
    elseif getPlayerStorageValue(cid, storage) >= os.time() then
        doPlayerSendTextMessage(cid,20, 'Voce precisa esperar 20 horas por tentativa. Faltam: ' .. getPlayerStorageValue(cid, storage) - os.time()..' segundos pra tentar novamente.') -- Checks if the player has already used the lever.
        doSendMagicEffect(getCreaturePosition(cid), 2)
        return false
    else
        for x = from.x, to.x do
            for y = from.y, to.y do
                local creature = getTopCreature({x = x, y = y, z = from.z})
                if isPlayer(creature.uid) and creature.uid ~= 0 then
                    doSendMagicEffect(getCreaturePosition(cid), 2)
                    doPlayerSendTextMessage(cid,20,"Ja existe um jogador dentro da quest. Aguarde sua vez.") -- Checks if there is someone inside already.
                    return true
                end
            end
        end
    local monster = doSummonCreature("grand master oberon", {x = 1643, y = 1580, z = 8})
    doTeleportThing(cid, newPos)
    doPlayerSendTextMessage(cid,20,"Voce tem 5 minutos para matar o Grand Master Oberon. Apos 5 minutos a criatura ira sumir e voce sera teleportado.")
    setPlayerStorageValue(cid, storage, os.time() + cooldown)
    addEvent(doRemoveCreature, 5 * 60 * 1000, monster) -- 5 min
    local function teleport()
            doTeleportThing(cid, afterPos)
            stopEvent(event)
            return true
        end
        event = addEvent(teleport, kickTime)
    return true
    end
end

[code]
 
Solution
For the remove creature it seems to have worked out fine. Thank you.
And yeah, for the teleport, i think i'm gonna try giving the player a storage for the Kick time, that way players can't stay in the room forever preventing other players from entering, and i'm gonna remove said storage with a teleporter, if the player wishes to leave eariler. I think that ought to work.

@Edit, couldn't get it to work. Here's what i did:

action
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)

local from, to = {x = 1635, y = 1574, z = 8}, {x = 1538, y = 1620, z = 8}
local newPos = {x = 1643, y = 1585, z = 8}
local afterPos = {x = 1634, y = 1604, z = 8}
local kickTime = 6 * 60 * 1000 -- 6 Minutes
local kStorage = 10124
local cooldown...

Kayzuu

New Member
Joined
Feb 5, 2021
Messages
3
Solutions
1
Reaction score
0
in remove creature case, try using addevent like this:

Lua:
addEvent(function()
    if isMonster(monster) then
        doRemoveCreature(monster)
    end
end, 5 *60 * 1000)

in the player teleport case, you can create a movements for the teleport that set a storage for the player to check if the player entered the teleport. or you can create a creaturescript for when you kill the boss you receive a storage and get teleported.
 
Last edited:
OP
OP
juansanchez

juansanchez

Well-Known Member
Joined
Apr 2, 2015
Messages
206
Reaction score
84
in remove creature case, try using addevent like this:

Lua:
addEvent(function()
    if isMonster(monster) then
        doRemoveCreature(monster)
    end
end, 5 *60 * 1000)

in the player teleport case, you can create a movements for the teleport that set a storage for the player to check if the player entered the teleport. or you can create a creaturescript for when you kill the boss you receive a storage and get teleported.
For the remove creature it seems to have worked out fine. Thank you.
And yeah, for the teleport, i think i'm gonna try giving the player a storage for the Kick time, that way players can't stay in the room forever preventing other players from entering, and i'm gonna remove said storage with a teleporter, if the player wishes to leave eariler. I think that ought to work.

@Edit, couldn't get it to work. Here's what i did:

action
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)

local from, to = {x = 1635, y = 1574, z = 8}, {x = 1538, y = 1620, z = 8}
local newPos = {x = 1643, y = 1585, z = 8}
local afterPos = {x = 1634, y = 1604, z = 8}
local kickTime = 6 * 60 * 1000 -- 6 Minutes
local kStorage = 10124
local cooldown = 720000
local reset = 60
local storage = 10123

    if getPlayerReset(cid) < reset then
        doSendMagicEffect(getCreaturePosition(cid), 2)
        return doPlayerSendTextMessage(cid,20, 'Voce precisa de '.. reset ..' resets ou mais.') -- Checks if the player has level to use the lever.
    elseif getPlayerStorageValue(cid, storage) >= os.time() then
        doPlayerSendTextMessage(cid,20, 'Voce precisa esperar 20 horas por tentativa. Faltam: ' .. getPlayerStorageValue(cid, storage) - os.time()..' segundos pra tentar novamente.') -- Checks if the player has already used the lever.
        doSendMagicEffect(getCreaturePosition(cid), 2)
        return false
    else
        for x = from.x, to.x do
            for y = from.y, to.y do
                local creature = getTopCreature({x = x, y = y, z = from.z})
                if isPlayer(creature.uid) and creature.uid ~= 0 then
                    doSendMagicEffect(getCreaturePosition(cid), 2)
                    doPlayerSendTextMessage(cid,20,"Ja existe um jogador dentro da quest. Aguarde sua vez.") -- Checks if there is someone inside already.
                    return true
                end
            end
        end
    local monster = doSummonCreature("grand master oberon", {x = 1643, y = 1580, z = 8})
    doTeleportThing(cid, newPos)
    doPlayerSendTextMessage(cid,20,"Voce tem 5 minutos para matar o Grand Master Oberon. Apos 5 minutos a criatura ira sumir e voce sera teleportado.")
    setPlayerStorageValue(cid, storage, os.time() + cooldown)
    setPlayerStorageValue(cid, kStorage, 1)
    addEvent(function()
        if isMonster(monster) then
            doRemoveCreature(monster)
        end
    end, 5 *60 * 1000) -- 5 min
    if getPlayerStorageValue (cid, kStorage) == 1 then
        local function teleport()
            setPlayerStorageValue(cid, kStorage, 0)
            doTeleportThing(cid, afterPos)
            stopEvent(event)
            return true
        end
            event = addEvent(teleport, kickTime)
        return true
    end
    end
end


movement

Lua:
local pos = {x = 1634, y = 1604, z = 8}

function onStepIn(cid, item, position, fromPosition)
if getPlayerAccess(cid) then
doTeleportThing(cid, pos)
setPlayerStorageValue (cid, 10124, 0)
doSendMagicEffect(getThingPos(cid), 37)
end
return true
end

Help plz?
 
Last edited:

Kayzuu

New Member
Joined
Feb 5, 2021
Messages
3
Solutions
1
Reaction score
0
For the remove creature it seems to have worked out fine. Thank you.
And yeah, for the teleport, i think i'm gonna try giving the player a storage for the Kick time, that way players can't stay in the room forever preventing other players from entering, and i'm gonna remove said storage with a teleporter, if the player wishes to leave eariler. I think that ought to work.

@Edit, couldn't get it to work. Here's what i did:

action
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)

local from, to = {x = 1635, y = 1574, z = 8}, {x = 1538, y = 1620, z = 8}
local newPos = {x = 1643, y = 1585, z = 8}
local afterPos = {x = 1634, y = 1604, z = 8}
local kickTime = 6 * 60 * 1000 -- 6 Minutes
local kStorage = 10124
local cooldown = 720000
local reset = 60
local storage = 10123

    if getPlayerReset(cid) < reset then
        doSendMagicEffect(getCreaturePosition(cid), 2)
        return doPlayerSendTextMessage(cid,20, 'Voce precisa de '.. reset ..' resets ou mais.') -- Checks if the player has level to use the lever.
    elseif getPlayerStorageValue(cid, storage) >= os.time() then
        doPlayerSendTextMessage(cid,20, 'Voce precisa esperar 20 horas por tentativa. Faltam: ' .. getPlayerStorageValue(cid, storage) - os.time()..' segundos pra tentar novamente.') -- Checks if the player has already used the lever.
        doSendMagicEffect(getCreaturePosition(cid), 2)
        return false
    else
        for x = from.x, to.x do
            for y = from.y, to.y do
                local creature = getTopCreature({x = x, y = y, z = from.z})
                if isPlayer(creature.uid) and creature.uid ~= 0 then
                    doSendMagicEffect(getCreaturePosition(cid), 2)
                    doPlayerSendTextMessage(cid,20,"Ja existe um jogador dentro da quest. Aguarde sua vez.") -- Checks if there is someone inside already.
                    return true
                end
            end
        end
    local monster = doSummonCreature("grand master oberon", {x = 1643, y = 1580, z = 8})
    doTeleportThing(cid, newPos)
    doPlayerSendTextMessage(cid,20,"Voce tem 5 minutos para matar o Grand Master Oberon. Apos 5 minutos a criatura ira sumir e voce sera teleportado.")
    setPlayerStorageValue(cid, storage, os.time() + cooldown)
    setPlayerStorageValue(cid, kStorage, 1)
    addEvent(function()
        if isMonster(monster) then
            doRemoveCreature(monster)
        end
    end, 5 *60 * 1000) -- 5 min
    if getPlayerStorageValue (cid, kStorage) == 1 then
        local function teleport()
            setPlayerStorageValue(cid, kStorage, 0)
            doTeleportThing(cid, afterPos)
            stopEvent(event)
            return true
        end
            event = addEvent(teleport, kickTime)
        return true
    end
    end
end


movement

Lua:
local pos = {x = 1634, y = 1604, z = 8}

function onStepIn(cid, item, position, fromPosition)
if getPlayerAccess(cid) then
doTeleportThing(cid, pos)
setPlayerStorageValue (cid, 10124, 0)
doSendMagicEffect(getThingPos(cid), 37)
end
return true
end

Help plz?
I think this should work:

Action
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
    local from, to = {x = 1635, y = 1574, z = 8}, {x = 1538, y = 1620, z = 8}
    local newPos = {x = 1643, y = 1585, z = 8}
    local afterPos = {x = 1634, y = 1604, z = 8}
    local kickTime = 6 * 60 * 1000 -- 6 Minutes
    local kStorage = 10124
    local cooldown = 720000
    local reset = 60
    local storage = 10123
  
    if getPlayerReset(cid) < reset then
        doSendMagicEffect(getCreaturePosition(cid), 2)
        return doPlayerSendTextMessage(cid,20, 'Voce precisa de '.. reset ..' resets ou mais.') -- Checks if the player has level to use the lever.
    elseif getPlayerStorageValue(cid, storage) >= os.time() then
        doPlayerSendTextMessage(cid,20, 'Voce precisa esperar 20 horas por tentativa. Faltam: ' .. getPlayerStorageValue(cid, storage) - os.time()..' segundos pra tentar novamente.') -- Checks if the player has already used the lever.
        doSendMagicEffect(getCreaturePosition(cid), 2)
        return false
    else
        for x = from.x, to.x do
            for y = from.y, to.y do
                local creature = getTopCreature({x = x, y = y, z = from.z})
                if isPlayer(creature.uid) and creature.uid ~= 0 then
                    doSendMagicEffect(getCreaturePosition(cid), 2)
                    doPlayerSendTextMessage(cid,20,"Ja existe um jogador dentro da quest. Aguarde sua vez.") -- Checks if there is someone inside already.
                    return true
                end
            end
        end
        local monster = doSummonCreature("grand master oberon", {x = 1643, y = 1580, z = 8})
        setPlayerStorageValue(cid, kStorage, -1) -- Resets player teleport check storage for preventing bugs.
        doTeleportThing(cid, newPos)
        doPlayerSendTextMessage(cid,20,"Voce tem 5 minutos para matar o Grand Master Oberon. Apos 5 minutos a criatura ira sumir e voce sera teleportado.")
        setPlayerStorageValue(cid, storage, os.time() + cooldown)
        addEvent(function()
            if isMonster(monster) then
                doRemoveCreature(monster)
            end
        end, 5 *60 * 1000) -- 5 min
        addEvent(function()
            if getPlayerStorageValue(cid, kStorage) ~= 0 then -- Check if the player doesn't have the storage that he receives when steps in teleport.
                doTeleportThing(cid, afterPos) -- If doesn't have, then get teleported out the room. Else, if he has, it means that he isn't in the room because he went in teleport and don't need tp
            end
        end, kickTime)
        return true
    end
end

Movement
Lua:
local pos = {x = 1634, y = 1604, z = 8}

function onStepIn(cid, item, position, fromPosition)
    doTeleportThing(cid, pos)
    setPlayerStorageValue (cid, 10124, 0)
    doSendMagicEffect(getThingPos(cid), 37)
    return true
end
 
Last edited:
Solution
OP
OP
juansanchez

juansanchez

Well-Known Member
Joined
Apr 2, 2015
Messages
206
Reaction score
84
I think this should work:

Action
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
    local from, to = {x = 1635, y = 1574, z = 8}, {x = 1538, y = 1620, z = 8}
    local newPos = {x = 1643, y = 1585, z = 8}
    local afterPos = {x = 1634, y = 1604, z = 8}
    local kickTime = 6 * 60 * 1000 -- 6 Minutes
    local kStorage = 10124
    local cooldown = 720000
    local reset = 60
    local storage = 10123
 
    if getPlayerReset(cid) < reset then
        doSendMagicEffect(getCreaturePosition(cid), 2)
        return doPlayerSendTextMessage(cid,20, 'Voce precisa de '.. reset ..' resets ou mais.') -- Checks if the player has level to use the lever.
    elseif getPlayerStorageValue(cid, storage) >= os.time() then
        doPlayerSendTextMessage(cid,20, 'Voce precisa esperar 20 horas por tentativa. Faltam: ' .. getPlayerStorageValue(cid, storage) - os.time()..' segundos pra tentar novamente.') -- Checks if the player has already used the lever.
        doSendMagicEffect(getCreaturePosition(cid), 2)
        return false
    else
        for x = from.x, to.x do
            for y = from.y, to.y do
                local creature = getTopCreature({x = x, y = y, z = from.z})
                if isPlayer(creature.uid) and creature.uid ~= 0 then
                    doSendMagicEffect(getCreaturePosition(cid), 2)
                    doPlayerSendTextMessage(cid,20,"Ja existe um jogador dentro da quest. Aguarde sua vez.") -- Checks if there is someone inside already.
                    return true
                end
            end
        end
        local monster = doSummonCreature("grand master oberon", {x = 1643, y = 1580, z = 8})
        setPlayerStorageValue(cid, kStorage, -1) -- Resets player teleport check storage for preventing bugs.
        doTeleportThing(cid, newPos)
        doPlayerSendTextMessage(cid,20,"Voce tem 5 minutos para matar o Grand Master Oberon. Apos 5 minutos a criatura ira sumir e voce sera teleportado.")
        setPlayerStorageValue(cid, storage, os.time() + cooldown)
        addEvent(function()
            if isMonster(monster) then
                doRemoveCreature(monster)
            end
        end, 5 *60 * 1000) -- 5 min
        addEvent(function()
            if getPlayerStorageValue(cid, kStorage) ~= 0 then -- Check if the player doesn't have the storage that he receives when steps in teleport.
                doTeleportThing(cid, afterPos) -- If doesn't have, then get teleported out the room. Else, if he has, it means that he isn't in the room because he went in teleport and don't need tp
            end
        end, kickTime)
        return true
    end
end

Movement
Lua:
local pos = {x = 1634, y = 1604, z = 8}

function onStepIn(cid, item, position, fromPosition)
    doTeleportThing(cid, pos)
    setPlayerStorageValue (cid, 10124, 0)
    doSendMagicEffect(getThingPos(cid), 37)
    return true
end
Ay man! Thank you! It seems to have worked perfectly.
I will run some more tests but it seems to work. Thank you all for the help
 

Itutorial

Premium User
Premium User
Joined
Dec 23, 2014
Messages
1,958
Solutions
52
Reaction score
650
Any local thats don't need to be set inside the function should be created outside of it. This way they aren't set every time the player uses the item. Also try to end if statments without elseifs or elses if the logic isn't required. That is more for cleanliness of the code.

here is an example
Lua:
local from, to = {x = 1635, y = 1574, z = 8}, {x = 1538, y = 1620, z = 8}
local newPos = {x = 1643, y = 1585, z = 8}
local afterPos = {x = 1634, y = 1604, z = 8}
local kickTime = 6 * 60 * 1000 -- 6 Minutes
local kStorage = 10124
local cooldown = 720000
local reset = 60
local storage = 10123

function onUse(cid, item, fromPosition, itemEx, toPosition)
    if getPlayerReset(cid) < reset then
        doSendMagicEffect(getCreaturePosition(cid), 2)
        return doPlayerSendTextMessage(cid,20, 'Voce precisa de '.. reset ..' resets ou mais.') -- Checks if the player has level to use the lever.
    end
   
    if getPlayerStorageValue(cid, storage) >= os.time() then
        doPlayerSendTextMessage(cid,20, 'Voce precisa esperar 20 horas por tentativa. Faltam: ' .. getPlayerStorageValue(cid, storage) - os.time()..' segundos pra tentar novamente.') -- Checks if the player has already used the lever.
        doSendMagicEffect(getCreaturePosition(cid), 2)
        return false
    end
   
    for x = from.x, to.x do
        for y = from.y, to.y do
            local creature = getTopCreature({x = x, y = y, z = from.z})
           
            if isPlayer(creature.uid) and creature.uid ~= 0 then
                doSendMagicEffect(getCreaturePosition(cid), 2)
                doPlayerSendTextMessage(cid,20,"Ja existe um jogador dentro da quest. Aguarde sua vez.") -- Checks if there is someone inside already.
            return true
            end
        end
    end
   
    local monster = doSummonCreature("grand master oberon", {x = 1643, y = 1580, z = 8})
    setPlayerStorageValue(cid, kStorage, -1) -- Resets player teleport check storage for preventing bugs.
    doTeleportThing(cid, newPos)
    doPlayerSendTextMessage(cid,20,"Voce tem 5 minutos para matar o Grand Master Oberon. Apos 5 minutos a criatura ira sumir e voce sera teleportado.")
    setPlayerStorageValue(cid, storage, os.time() + cooldown)
   
    addEvent(function()
    if isMonster(monster) then
        doRemoveCreature(monster)
    end
    end, 5 *60 * 1000) -- 5 min
   
    addEvent(function()
    if getPlayerStorageValue(cid, kStorage) ~= 0 then -- Check if the player doesn't have the storage that he receives when steps in teleport.
        doTeleportThing(cid, afterPos) -- If doesn't have, then get teleported out the room. Else, if he has, it means that he isn't in the room because he went in teleport and don't need tp
    end
    end, kickTime)
   
    return true
end
 
OP
OP
juansanchez

juansanchez

Well-Known Member
Joined
Apr 2, 2015
Messages
206
Reaction score
84
Any local thats don't need to be set inside the function should be created outside of it. This way they aren't set every time the player uses the item. Also try to end if statments without elseifs or elses if the logic isn't required. That is more for cleanliness of the code.

here is an example
Lua:
local from, to = {x = 1635, y = 1574, z = 8}, {x = 1538, y = 1620, z = 8}
local newPos = {x = 1643, y = 1585, z = 8}
local afterPos = {x = 1634, y = 1604, z = 8}
local kickTime = 6 * 60 * 1000 -- 6 Minutes
local kStorage = 10124
local cooldown = 720000
local reset = 60
local storage = 10123

function onUse(cid, item, fromPosition, itemEx, toPosition)
    if getPlayerReset(cid) < reset then
        doSendMagicEffect(getCreaturePosition(cid), 2)
        return doPlayerSendTextMessage(cid,20, 'Voce precisa de '.. reset ..' resets ou mais.') -- Checks if the player has level to use the lever.
    end
  
    if getPlayerStorageValue(cid, storage) >= os.time() then
        doPlayerSendTextMessage(cid,20, 'Voce precisa esperar 20 horas por tentativa. Faltam: ' .. getPlayerStorageValue(cid, storage) - os.time()..' segundos pra tentar novamente.') -- Checks if the player has already used the lever.
        doSendMagicEffect(getCreaturePosition(cid), 2)
        return false
    end
  
    for x = from.x, to.x do
        for y = from.y, to.y do
            local creature = getTopCreature({x = x, y = y, z = from.z})
          
            if isPlayer(creature.uid) and creature.uid ~= 0 then
                doSendMagicEffect(getCreaturePosition(cid), 2)
                doPlayerSendTextMessage(cid,20,"Ja existe um jogador dentro da quest. Aguarde sua vez.") -- Checks if there is someone inside already.
            return true
            end
        end
    end
  
    local monster = doSummonCreature("grand master oberon", {x = 1643, y = 1580, z = 8})
    setPlayerStorageValue(cid, kStorage, -1) -- Resets player teleport check storage for preventing bugs.
    doTeleportThing(cid, newPos)
    doPlayerSendTextMessage(cid,20,"Voce tem 5 minutos para matar o Grand Master Oberon. Apos 5 minutos a criatura ira sumir e voce sera teleportado.")
    setPlayerStorageValue(cid, storage, os.time() + cooldown)
  
    addEvent(function()
    if isMonster(monster) then
        doRemoveCreature(monster)
    end
    end, 5 *60 * 1000) -- 5 min
  
    addEvent(function()
    if getPlayerStorageValue(cid, kStorage) ~= 0 then -- Check if the player doesn't have the storage that he receives when steps in teleport.
        doTeleportThing(cid, afterPos) -- If doesn't have, then get teleported out the room. Else, if he has, it means that he isn't in the room because he went in teleport and don't need tp
    end
    end, kickTime)
  
    return true
end

Thanks for the tips man, i really appreciate it.
When i started this code i thought about what u said about the locals, but i saw another similar script and i based myself on it, but what you said makes a lot more sense haha.
And yeah, the IFs not only look cleaner like this, but you can understand the code better. Thanks again man :D i will keep all this in mind for next projects.
 
Top