• 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 Modify this script to avoid error?

mRefaat

Marketing and Coding
Joined
Jan 18, 2014
Messages
872
Solutions
3
Reaction score
151
Location
Egypt
Hello,

When I try to modify position in the script I get this error

this is the script without any errors
Lua:
local t = {
 arena = {
 {x = 1532, y = 2618, z = 7}, -- North west Corner of Arena
 {x = 1552, y = 2638, z = 7}, -- South East corner of Arena
 {x = 1542, y = 2627, z = 7} -- Center of Arena
 },

 minPlayers = 1, -- min players required to start the battle
 noPlayers = 1, -- Leave it as it is
 prize = {6527} -- Reward that player recives
 }
 local kick = 0

 function onThink()
 local arenaPlayers = {}

 for x = t.arena[1].x, t.arena[2].x do
 for y = t.arena[1].y, t.arena[2].y do
 for z = t.arena[1].z, t.arena[2].z do
 local pos = {x = x, y = y, z = z}
 local n = getTileInfo(pos).creatures
 if n ~= 0 then
 pos.stackpos = 1
 local c = getThingfromPos(pos)
 while c.uid ~= 0 do
 if c.itemid == 1 and c.type == 1 then
 table.insert(arenaPlayers, c.uid)
 if #arenaPlayers == n then
 break
 end
 end
 pos.stackpos = pos.stackpos + 1
 c = getThingfromPos(pos)
 end
 end
 end
 end
 end

 if #arenaPlayers == 1 then
 local p = getPlayerMasterPos(arenaPlayers[1])
 doTeleportThing(arenaPlayers[1], p)
 doSendMagicEffect(p, CONST_ME_TELEPORT)
 doPlayerSendTextMessage(arenaPlayers[1], MESSAGE_STATUS_CONSOLE_BLUE, "You have won the event and received your reward.")
 doBroadcastMessage(getCreatureName(arenaPlayers[1]) .." won a Last Man Standing Event.")
 doPlayerAddItem(arenaPlayers[1], t.prize[math.random(#t.prize)], 10)
 kick = 0
 elseif #arenaPlayers > 1 then
 if kick == 0 then
 kick = os.time()
 else
 if os.time() - kick >= 840 then
 kick = 0
 for i = 1, #arenaPlayers do
 doTeleportThing(arenaPlayers[i], {x=2001, y=2030, z=7})
 doPlayerSendTextMessage(arenaPlayers[i], MESSAGE_STATUS_WARNING, "Too even, try harder next time.")
 end
 end
 end
 elseif #arenaPlayers == 0 then
 kick = 0
 end
 return true
 end

When I try to change this part

Lua:
 arena = {
 {x = 1532, y = 2618, z = 7}, -- North west Corner of Arena
 {x = 1552, y = 2638, z = 7}, -- South East corner of Arena
 {x = 1542, y = 2627, z = 7} -- Center of Arena
 },

to be like this

Code:
 arena = {
 {x = 1532, y = 2618, z = 6}, -- North west Corner of Arena
 {x = 1552, y = 2638, z = 8}, -- South East corner of Arena
 {x = 1542, y = 2627, z = 7} -- Center of Arena
 },

i get this error

Code:
[15:57:03.539] [Error - GlobalEvent Interface]
[15:57:03.544] data/globalevents/scripts/arena.lua:onThink
[15:57:03.550] Description:
[15:57:03.551] data/globalevents/scripts/arena.lua:21: attempt to index a boolean value
[15:57:03.559] stack traceback:
[15:57:03.561]  data/globalevents/scripts/arena.lua:21: in function <data/globalevents/scripts/arena.lua:14>
[15:57:03.571] [Error - GlobalEvents::think] Couldn't execute event: arena

can someone tell me what is the problem? i want to add range from z to z also

also i want to ask with this part mean ?
Code:
if kick == 0 then
 kick = os.time()
 else

using tfs 0.3.7 rev 5969
 
This part:
Lua:
 local pos = {x = x, y = y, z = z}
local n = getTileInfo(pos).creatures
if n ~= 0 then
    pos.stackpos = 1
    local c = getThingfromPos(pos)
    while c.uid ~= 0 do
        if c.itemid == 1 and c.type == 1 then
            table.insert(arenaPlayers, c.uid)
            if #arenaPlayers == n then
                break
            end
        end
        pos.stackpos = pos.stackpos + 1
        c = getThingfromPos(pos)
     end
end

Is executed (1552 - 1532 + 1) * (2638 - 2618 + 1) * (8 - 6 + 1) times everytime you execute this globalevent. There are much better ways to check such a large amount of tiles.

This part:
Lua:
pos.stackpos = pos.stackpos + 1
Can check single pos for infinity, stackpos is 0-255 but your code will spam even if 10000000+

All tiles cannot be void (black tiles). That's why you have errors. Fix it in map editor

ill give you part of my arena system:
Lua:
function getPlayersInArenaLMS(arena)
    local pos, size = lmsGlobalConfig.arenas[arena].arenaPos, (lmsGlobalConfig.maxArenaSqmSize / 2) + 3
    local spec = getSpectators({x = pos[1], y = pos[2], z = pos[3]}, size, size)
    local t = {}
    if spec ~= nil and #spec > 0 then
        for i = 1, #spec do
            if isPlayer(spec[i]) == true then
                if lmsGlobalConfig.ignoreGM == true then
                    if getPlayerGroupId(spec[i]) <= lmsGlobalConfig.maximumGroupID then
                        table.insert(t, spec[i])
                    end
                else
                    table.insert(t, spec[i])
                end
            end
        end
    end
    return t
end

just edit it to match your needs, it returns table with all players inside

Instead of spamming your script for thousands times per each onThink you have to use getSpectators and it will check only once. Saving alot of lags

getSpectators(x,y,z, rangex, rangey) returns a full table with all players in rangex and rangey away from center x,y,z

rangex 3 and rangey 1 will looks like this, where C is center with coords x,y,z:

o,o,o,o,o,o,o
o,o,o,C,o,o,o
o,o,o,o,o,o,o

Doesn't matter if map editor will fix your errors, your script have a serious issue with infinity spam, the longer you will use it the heavier your server will lag. Ofc without any errors. After some time something starts breaking, potions, runes, simple scripts, monsters etc and then finally your OS will kill it or it will perma freeze. So you will have to restart your PC or the hosting machine. In 90s your script might be able to toast almost every PC
 
Last edited:
This part:
Lua:
 local pos = {x = x, y = y, z = z}
local n = getTileInfo(pos).creatures
if n ~= 0 then
    pos.stackpos = 1
    local c = getThingfromPos(pos)
    while c.uid ~= 0 do
        if c.itemid == 1 and c.type == 1 then
            table.insert(arenaPlayers, c.uid)
            if #arenaPlayers == n then
                break
            end
        end
        pos.stackpos = pos.stackpos + 1
        c = getThingfromPos(pos)
     end
end

Is executed (1552 - 1532 + 1) * (2638 - 2618 + 1) * (8 - 6 + 1) times everytime you execute this globalevent. There are much better ways to check such a large amount of tiles.

This part:
Lua:
pos.stackpos = pos.stackpos + 1
Can check single pos for infinity, stackpos is 0-255 but your code will spam even if 10000000+

All tiles cannot be void (black tiles). That's why you have errors. Fix it in map editor

ill give you part of my arena system:
Lua:
function getPlayersInArenaLMS(arena)
    local pos, size = lmsGlobalConfig.arenas[arena].arenaPos, (lmsGlobalConfig.maxArenaSqmSize / 2) + 3
    local spec = getSpectators({x = pos[1], y = pos[2], z = pos[3]}, size, size)
    local t = {}
    if spec ~= nil and #spec > 0 then
        for i = 1, #spec do
            if isPlayer(spec[i]) == true then
                if lmsGlobalConfig.ignoreGM == true then
                    if getPlayerGroupId(spec[i]) <= lmsGlobalConfig.maximumGroupID then
                        table.insert(t, spec[i])
                    end
                else
                    table.insert(t, spec[i])
                end
            end
        end
    end
    return t
end

just edit it to match your needs, it returns table with all players inside

Instead of spamming your script for thousands times per each onThink you have to use getSpectators and it will check only once. Saving alot of lags

getSpectators(x,y,z, rangex, rangey) returns a full table with all players in rangex and rangey away from center x,y,z

rangex 3 and rangey 1 will looks like this, where C is center with coords x,y,z:

o,o,o,o,o,o,o
o,o,o,C,o,o,o
o,o,o,o,o,o,o

Doesn't matter if map editor will fix your errors, your script have a serious issue with infinity spam, the longer you will use it the heavier your server will lag. Ofc without any errors. After some time something starts breaking, potions, runes, simple scripts, monsters etc and then finally your OS will kill it or it will perma freeze. So you will have to restart your PC or the hosting machine. In 90s your script might be able to toast almost every PC
Thanks for your instructions :)
I used another script posted by printer and i modified it to be automatic and it works perfect till now.
 
Back
Top