• 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 1.2 CTF tested (not thoroughly)

Itutorial

Excellent OT User
Joined
Dec 23, 2014
Messages
2,307
Solutions
68
Reaction score
982
Place the flags set their AID to the one defined in the CTFlib file set all required information in the lib. (if you scroll down in the lib you can edit rewards) Have fun....Its not testing to 100% ability but I did test the majority of it and it works.

in data/ make a file called ctf.lua

Code:
config = {
["event"] = 50000,

["green_team"] = 50001,
["red_team"] = 50002,

["score_red_team"] = 50003,
["score_green_team"] = 50004,

["player_has_red_flag"] = 50005,
["player_has_green_flag"] = 50006,

["p_green_team"] = 50007,
["p_red_team"] = 50008,

["cycle"] = 50009,

["max_players_per_team"] = 10,
["required_players"] = 10,
["max_flags_captured"] = 10,
["end_automatic"] = true, --Should it end CTF by itself?
["end_automatically_minutes"] = 30,

["green_flag_pos"] = {x = 1301, y = 1739, z = 7},
["red_flag_pos"] = {x = 1294, y = 1739, z = 7},

["green_flag_itemid"] = 1437,
["red_flag_itemid"] = 1435,
["flag_aid"] = 3000,

["no_flag_itemid"] = 1642, --set this to item that pops up when flag is taken

["red_team_start_pos"] = {x = 1294, y = 1740, z = 7},
["green_team_start_pos"] = {x = 1301, y = 1740, z = 7},

["temple"] = {x = 1120, y = 1778, z = 7}
}

function openCTF()
Game.setStorageValue(config["event"], 1)
Game.broadcastMessage("CTF is now open. Type !joinCTF to enter.")
end

function playerJoinTeam(player)
    if Game.getStorageValue(config["event"]) == 1 or Game.getStorageValue(config["event"]) == 2 then
        if Game.getStorageValue(config["red_team"]) > config["max_players_per_team"] and Game.getStorageValue(config["green_team"]) > config["max_players_per_team"] then
        return    player:sendCancelMessage("The teams are full.")
        end

        if Game.getStorageValue(config["red_team"]) <= Game.getStorageValue(config["green_team"]) and Game.getStorageValue(config["red_team"]) <= config["max_players_per_team"] then
            player:setStorageValue(config["p_red_team"], 1)
            Game.setStorageValue(config["red_team"], Game.getStorageValue(config["red_team"]) + 1)
            player:sendTextMessage(4, "You have joined the red team. CTF will start when enough players have joined. Type !leaveCTF to exit the queue")
        elseif Game.getStorageValue(config["green_team"]) <= Game.getStorageValue(config["red_team"]) and Game.getStorageValue(config["green_team"]) <= config["max_players_per_team"] then
            player:setStorageValue(config["[p_green_team"], 1)
            Game.setStorageValue(config["green_team"], Game.getStorageValue(config["green_team"]) + 1)
            player:sendTextMessage(4, "You have joined the green team. CTF will start when enough players have joined. Type !leaveCTF to exit the queue")
        end
    else
        return player:sendCancelMessage("CTF is not open.")
    end
end


function checkCTF()
if Game.getStorageValue(config["event"]) == 1 then
    red_team = Game.getStorageValue(config["red_team"])
    green_team = Game.getStorageValue(config["green_team"])

    if red_team + green_team >= config["required_players"] then
        Game.broadcastMessage("CTF will start in 5 minutes.")
        Game.setStorageValue(config["event"], 2)
        Game.setStorageValue(config["cycle"], 1)
        addEvent(nextStep, 60 * 1000)
    end
end
end

function nextStep()
    if Game.getStorageValue(config["event"]) == 2 then
        red_team = Game.getStorageValue(config["red_team"])
        green_team = Game.getStorageValue(config["green_team"])

        if red_team + green_team < config["required_players"] then
            Game.broadcastMessage("Players have left the CTF event queue. There are not enough players for the event to start.")
            Game.setStorageValue(config["event"], 1)
        return false
        end
      
        if Game.getStorageValue(config["cycle"]) < 5 then
            time_left = 5 - Game.getStorageValue(config["cycle"])
            Game.broadcastMessage("CTF will start in "..time_left.." minutes.")
            Game.setStorageValue(config["cycle"], Game.getStorageValue(config["cycle"]) + 1)
            addEvent(nextStep, 60 * 1000)
        else
            Game.setStorageValue(config["event"], 3)
            Game.setStorageValue(config["cycle"], 0)
            addEvent(startCTF, 0)
        end
    end
end

function startCTF()
    players = Game.getPlayers()
    for i = 1, #players do
        player = Player(players[i])
        if player:getStorageValue(config["p_red_team"]) == 1 then
            player:teleportTo(config["red_team_start_pos"])
            player:sendTextMessage(4, "CTF has started.")
        elseif player:getStorageValue(config["p_green_team"]) == 1 then
            player:teleportTo(config["green_team_start_pos"])
            player:sendTextMessage(4, "CTF has started.")
        end
    end
    Game.setStorageValue(config["score_green_team"], 0)
    Game.setStorageValue(config["score_red_team"], 0)
    if config["end_automatic"] == true then
    addEvent(endCTF, config["end_automatically_minutes"] * 60 * 1000)
    end
end

function endCTF()
players = Game.getPlayers()
    if Game.getStorageValue(config["score_green_team"]) > Game.getStorageValue(config["score_red_team"]) then
        for i = 1, #players do
            player = Player(players[i])
                if player:getStorageValue(config["p_green_team"]) == 1 then
                    --Add rewards here
                    player:sendTextMessage(4, "Your team has won!")
                end
        end
    elseif Game.getStorageValue(config["score_green_team"]) < Game.getStorageValue(config["score_red_team"]) then
        for i = 1, #players do
            player = Player(players[i])
                if player:getStorageValue(config["p_red_team"]) == 1 then
                    --Add rewards here
                    player:sendTextMessage(4, "Your team has won!")
                end
        end
        Game.broadcastMessage("Red team has won CTF event!")
      
    elseif Game.getStorageValue(config["score_green_team"]) == Game.getStorageValue(config["score_red_team"]) then
        for i = 1, #players do
            player = Player(players[i])
                if player:getStorageValue(config["p_red_team"]) == 1 or player:getStorageValue(config["p_green_team"]) == 1 then
                    --Add tie rewards here
                    player:sendTextMessage(4, "There has been a tie!")
                end
        end
        Game.broadcastMessage("The teams have tied playing CTF!")
    end
  
for i = 1, #players do
    if player:getStorageValue(config["p_red_team"]) == 1 then
        player:setStorageValue(config["p_red_team"], 0)
        player:teleportTo(config["temple"])
    elseif player:getStorageValue(config["p_green_team"]) == 1 then
        player:setStorageValue(config["p_green_team"], 0)
        player:teleportTo(config["temple"])
    end
end

Game.setStorageValue(config["score_green_team"], 0)
Game.setStorageValue(config["score_red_team"], 0)
Game.setStorageValue(config["event"], 0)
Game.setStorageValue(config["cycle"], 0)
Game.setStorageValue(config["red_team"], 0)
Game.setStorageValue(config["green_team"], 0)
end

data/talkactions/scripts make a file called ctf.lua

Code:
dofile("data/ctf.lua")

function onSay(player, words)
    if (words == "!startCTF") then
    if not player:getGroup():getAccess() then
        return true
    end

    if player:getAccountType() < ACCOUNT_TYPE_GOD then
        return false
    end
    Game.setStorageValue(config["green_team"], 0)
    Game.setStorageValue(config["red_team"], 0)
    Game.setStorageValue(config["p_green_team"], 0)
    Game.setStorageValue(config["p_red_team"], 0)
    Game.setStorageValue(config["score_red_team"], 0)
    Game.setStorageValue(config["score_green_team"], 0)
    Game.setStorageValue(config["cycle"], 0)
    openCTF()

    elseif (words == "!joinCTF") then
    if player:getStorageValue(config["p_red_team"]) == 1 or player:getStorageValue(config["p_green_team"]) == 1 then
    return player:sendCancelMessage("You have already joined the CTF event.")
    end
    playerJoinTeam(player)
    elseif (words == "!leaveCTF") then
        if player:getStorageValue(config["p_green_team"]) == 1 then
            player:setStorageValue(config["p_green_team"], 0)
            Game.setStorageValue(config["green_team"], Game.getStorageValue(config["green_team"]) - 1)
            player:sendTextMessage(4, "You have left the event.")
        elseif player:getStorageValue(config["p_red_team"]) == 1 then
            player:setStorageValue(config["p_red_team"], 0)
            Game.setStorageValue(config["red_team"], Game.getStorageValue(config["red_team"]) - 1)
            player:sendTextMessage(4, "You have left the event.")
        else
        player:sendTextMessage(4, "You are not in CTF event.")
        end
    end
return false
end

talkactions.xml

Code:
<talkaction words="!startCTF" script="ctf.lua" />
<talkaction words="!joinCTF" script="ctf.lua" />
<talkaction words="!leaveCTF" script="ctf.lua" />

data/globalevents/scripts make a file called ctf.lua

Code:
dofile("data/ctf.lua")

function onThink(interval)
if Game.getStorageValue(config["event"]) == 1 then
    checkCTF()
end
return true
end

in globalevents.xml
Code:
<globalevent name="CTF" interval="10000" script="ctf.lua" />
 
Last edited:
in data/creaturescripts/ make a file called ctf.lua

Code:
dofile("data/ctf.lua")

function onDeath(player, corpse, killer, mostDamageKiller, unjustified, mostDamageUnjustified)
    if player:getStorageValue(config["p_green_team"]) == 1 then
        player:setDropLoot(false)
        player:teleportTo(config["green_team_start_pos"])
        player:addHealth(player:getMaxHealth())
            if player:getStorageValue(config["player_has_red_flag"]) == 1 then
                flag_pos = Tile(config["red_flag_pos"])
                flag = flag_pos:getItemById(config["no_flag_itemid"])
                flag:remove()
                flag_new = doCreateItem(config["red_flag_itemid"], 1, config["red_flag_pos"])
                flag_new:setItemAttribute('aid', config["flag_aid"])
                player:setStorageValue(config["player_has_red_flag"], 0)
            end
    elseif player:getStorageValue(config["p_red_team"]) == 1 then
        player:setDropLoot(false)
        player:teleportTo(config["red_team_start_pos"])
        player:addHealth(player:getMaxHealth())
            if player:getStorageValue(config["player_has_green_flag"]) == 1 then
                flag_pos = Tile(config["green_flag_pos"])
                flag = flag_pos:getItemById(config["no_flag_itemid"])
                flag:remove()
                flag_new = doCreateItem(config["green_flag_itemid"], 1, config["green_flag_pos"])
                flag_new:setItemAttribute('aid', config["flag_aid"])
                player:setStorageValue(config["player_has_green_flag"], 0)
            end
    end
end

open data/creaturescripts/logout and add this somewhere

Code:
if player:getStorageValue(config["p_green_team"]) == 1 then
    player:setStorageValue(config["p_green_team"], 0)
    Game.setStorageValue(config["green_team"], Game.getStorageValue(config["green_team"]) - 1)
elseif player:getStorageValue(config["p_red_team"]) == 1 then
    player:setStorageValue(config["p_red_team"], 0)
    Game.setStorageValue(config["p_red_team"], Game.getStorageValue(config["p_red_team"]) - 1)
end

in creaturescripts/login.lua add this

Code:
if player:getStorageValue(config["p_green_team"]) == 1 then
    player:setStorageValue(config["p_green_team"], 0)
    Game.setStorageValue(config["green_team"], Game.getStorageValue(config["green_team"]) - 1)
elseif player:getStorageValue(config["p_red_team"]) == 1 then
    player:setStorageValue(config["p_red_team"], 0)
    Game.setStorageValue(config["p_red_team"], Game.getStorageValue(config["p_red_team"]) - 1)
end

in creaturescripts.xml

Code:
<event type="death" name="CTF" script="ctf.lua"/>

in data/actions/scripts make a file called ctf.lua

Code:
dofile("data/ctf.lua")

function onUse(player, item, fromPosition, target, toPosition)
    if item.itemid == config["red_flag_itemid"] and item:getAttribute('aid') == config["flag_aid"] then
        if player:getStorageValue(config["p_red_team"]) == 1 and player:getStorageValue(config["player_has_green_flag"]) ~= 1 then
            return player:sendCancelMessage("You cannot take your own flag.")
        end
            if player:getStorageValue(config["player_has_green_flag"]) == 1 then
                player:setStorageValue(config["player_has_green_flag"], 0)
                Game.setStorageValue(config["score_red_team"], Game.getStorageValue(config["score_red_team"]) + 1)
                Game.broadcastMessage("Red team has scored a flag!")
                flag_pos = Tile(config["green_flag_pos"])
                flag = flag_pos:getItemById(config["no_flag_itemid"])
                flag:remove()
                no_flag = doCreateItem(config["green_flag_itemid"], 1, config["green_flag_pos"])
                item_flag_pos = Tile(config["green_flag_pos"])
                item_flag = item_flag_pos:getItemById(config["green_flag_itemid"])
                item_flag:setAttribute('aid', config["flag_aid"])
                if Game.getStorageValue(config["score_red_team"]) >= config["max_flags_captured"] then
                    endCTF()
                end
            end
        if player:getStorageValue(config["p_green_team"]) == 1 then
        player:setStorageValue(config["player_has_red_flag"], 1)
        flag_pos = Tile(config["red_flag_pos"])
        flag = flag_pos:getItemById(config["red_flag_itemid"])
        flag:remove()
        local no_flag = Game.createItem(config["no_flag_itemid"], 1, config["red_flag_pos"])
        no_flag:setAttribute('aid', config["flag_aid"])
        end
       
    elseif item.itemid == config["green_flag_itemid"] and item:getAttribute('aid') == config["flag_aid"] then
        if player:getStorageValue(config["p_green_team"]) == 1 and player:getStorageValue(config["player_has_red_flag"]) ~= 1 then
            return player:sendCancelMessage("You cannot take your own flag.")
        end
            if player:getStorageValue(config["player_has_red_flag"]) == 1 then
                player:setStorageValue(config["player_has_red_flag"], 0)
                Game.setStorageValue(config["score_green_team"], Game.getStorageValue(config["score_green_team"]) + 1)
                Game.broadcastMessage("Green team has scored a flag!")
                flag_pos = Tile(config["red_flag_pos"])
                flag = flag_pos:getItemById(config["no_flag_itemid"])
                flag:remove()
                no_flag = doCreateItem(config["red_flag_itemid"], 1, config["red_flag_pos"])
                item_flag_pos = Tile(config["red_flag_pos"])
                item_flag = item_flag_pos:getItemById(config["red_flag_itemid"])
                item_flag:setAttribute('aid', config["flag_aid"])
                if Game.getStorageValue(config["score_green_team"]) >= config["max_flags_captured"] then
                    endCTF()
                end
            end
        if player:getStorageValue(config["p_red_team"]) == 1 then
        player:setStorageValue(config["player_has_green_flag"], 1)
        flag_pos = Tile(config["green_flag_pos"])
        flag = flag_pos:getItemById(config["green_flag_itemid"])
        flag:remove()
        local no_flag = Game.createItem(config["no_flag_itemid"], 1, config["green_flag_pos"])
        no_flag:setAttribute('aid', config["flag_aid"])
        end
       
    elseif item.itemid == config["no_flag_itemid"] and item:getAttribute('aid') == config["flag_aid"] and item:getPosition() == config["red_flag_pos"] then
        if player:getStorageValue(config["p_green_team"]) == 1 then
            return player:sendCancelMessage("The red teams flag has already been captured.")
        end
       
        if player:getStorageValue(config["p_red_team"]) == 1 then
            if player:getStorageValue(config["player_has_green_flag"]) == 1 then
                return player:sendCancelMessage("Your flag must be returned before you can capture the enemies flag.")
            else
                return player:sendCancelMessage("Go get your flag!")
            end
        end
       
    elseif item.itemid == config["no_flag_itemid"] and item:getAttribute('aid') == config["flag_aid"] and item:getPosition() == config["green_flag_pos"] then
        if player:getStorageValue(config["p_red_team"]) == 1 then
            return player:sendCancelMessage("The green teams flag has already been captured.")
        end
       
        if player:getStorageValue(config["p_green_team"]) == 1 then
            if player:getStorageValue(config["player_has_red_flag"]) == 1 then
                return player:sendCancelMessage("Your flag must be returned before you can capture the enemies flag.")
            else
                return player:sendCancelMessage("Go get your flag!")
            end
        end
    end
return true
end

in actions.xml

Code:
<!-- CTF -->
    <action itemid="1435" script="custom/ctf.lua" />
    <action itemid="1437" script="custom/ctf.lua" />
    <action itemid="1642" script="custom/ctf.lua" /> <!--set this to item that pops up when flag is taken-->
 
for what reason do you use player storage values?
You could skip the entire onlogin part etc. if you just throw the informations in a temporary table instead of using player storages which can lead to errors if not removed well.
 
If you don't like it you can make a system yourself and release it.
 
If you don't like it you can make a system yourself and release it.
He isn't saying he doesn't like it, but rather inquiring why are you using storages.. but I do agree with you that if they seen something wrong with the script they should not only point out what they think is wrong but also show an improved updated version of it.
 
He isn't saying he doesn't like it, but rather inquiring why are you using storages.. but I do agree with you that if they seen something wrong with the script they should not only point out what they think is wrong but also show an improved updated version of it.
:D:rolleyes:
 
I created this code a long time ago and would make it completely different now. I might be willing to create the system again.
 
not working, the script start the event, but not balancead the teams only red team enter in this event and the flags not working on click. No error in distro
 
the event starts but nothing happens anybody have solution ?

Forgot to say ( No errors in console )
 
Last edited by a moderator:
the event starts but nothing happens anybody have solution ?

Forgot to say ( No errors in console )

not working, the script start the event, but not balancead the teams only red team enter in this event and the flags not working on click. No error in distro

Same here.

When clicking on enemy flag it shows error in console:
Code:
Lua Script Error: [Action Interface]
data/actions/scripts/other/ctf.lua:onUse
data/actions/scripts/other/ctf.lua:51: attempt to index a nil value
stack traceback:
        [C]: in function '__index'
        data/actions/scripts/other/ctf.lua:51: in function <data/actions/scripts/other/ctf.lua:1>

So I tried to do like this (following my working DemonHelmetLever.lua):
Lua:
local flag = Tile(Position(33442, 31289, 7))
        flag:getItemById(1437):remove()

But error persists.
 
I'm working on it using the latest TFS.
It looks like the lua files can't load the positions configured in the data/ctf.lua (in the lib of the system).

If you change every config["green_flag_pos"] for Position(33442, 31289, 7) it starts to work more things (now the flag transforms into stone).
 
Back
Top