• 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 to improve my script?

ausirosiris

Member
Joined
May 23, 2020
Messages
105
Reaction score
22
LUA:
local scores = {
    blue = 101,
        red = 102,

}
local teams = {
    blueName = "Blue",
        redName = "Red",
}
function onKill(cid)

    if getGlobalStorageValue(scores.blue) > getGlobalStorageValue(scores.red) then
            str = "" ..teams.blueName.. "/n" ..  getGlobalStorageValue(scores.blue) .. " vs" ..teams.redName.. getGlobalStorageValue(scores.red)..""
           
                elseif getGlobalStorageValue(scores.red) > getGlobalStorageValue(scores.blue) then
            str = "" ..teams.redName.. "" ..  getGlobalStorageValue(scores.red) .. " vs " ..teams.blueName.. getGlobalStorageValue(scores.blue)..""
           
           
                        elseif getGlobalStorageValue(scores.red) == getGlobalStorageValue(scores.blue) then  
            str = "" ..teams.blueName.. "" .. getGlobalStorageValue(scores.blue).. " vs " ..teams.redName.. getGlobalStorageValue(scores.red)..""
                                end
                               
                               
                                    doBroadcastMessage(str .. ".", MESSAGE_EVENT_RED)
                                        return true
                                                    end


How can i make this better/faster?! I mean, it's working but, if you make the first kill always shows as "Blue 1 vs -1 Red". Until they even the score the opposite side stays with a negative value.
(even with the globalevents version of it). Anyone can optimize or give me a light? Thanks;
 
Last edited:
Solution
Ideally, you organize the algorithm of your scripts in this way:
LUA:
local blueTeam, redTeam = 101, 102
local scores = {
    [1] = { team = blueTeam },
    [2] = { team = redTeam },
}

----- Init -----
function onThink(interval, lastExecution, thinkInterval)

    ---- Blue Team lead -----
    if getGlobalStorageValue(blueTeam) > getGlobalStorageValue(redTeam) then
        str = "Blue Team: " .. getGlobalStorageValue(blueTeam) .. " vs Red Team: " .. getGlobalStorageValue(redTeam)

        ----- Red Team Lead -----
    elseif getGlobalStorageValue(redTeam) > getGlobalStorageValue(blueTeam) then
        str = "Red Team: " .. getGlobalStorageValue(blueTeam) .. " vs Blue Team: " .. getGlobalStorageValue(redTeam)

        ----- Tied Score...
You need to set global storage value to 0 always your server startup.
Make a file into globalevents to this lua script:
LUA:
local blueTeam, redTeam = 101, 102
local scores = {
    [1] = { team = blueTeam },
    [2] = { team = redTeam },
}

function onStartup()
    for i = 1, #scores do
        setGlobalStorageValue(scores[i].team, 0)
    end

    return true
end
 
You need to set global storage value to 0 always your server startup.
Make a file into globalevents to this lua script:
LUA:
local blueTeam, redTeam = 101, 102
local scores = {
    [1] = { team = blueTeam },
    [2] = { team = redTeam },
}

function onStartup()
    for i = 1, #scores do
        setGlobalStorageValue(scores[i].team, 0)
    end

    return true
end
 
Last edited:
LUA:
function onStartup()
    setGlobalStorageValue(101, 0)
    setGlobalStorageValue(102, 0)

    return true
end
Oops. I'm sorry forgot to close the thread and answer you! But here is the final version of it that i've wrote. Thank you for the help!
LUA:
local blueTeam, redTeam = 101, 102
local scores = {
    [1] = { team = blueTeam },
    [2] = { team = redTeam },
}

----- Init -----
function onThink(interval, lastExecution, thinkInterval)

---- Blue Team lead -----
if getGlobalStorageValue(blueTeam) > getGlobalStorageValue(redTeam) then
 str = "Blue Team: "    ..  getGlobalStorageValue(blueTeam) .. " vs Red Team: " .. getGlobalStorageValue(redTeam)
     
----- Red Team Lead -----      
elseif getGlobalStorageValue(redTeam) > getGlobalStorageValue(blueTeam) then
 str = "Red Team: "    ..  getGlobalStorageValue(blueTeam) .. " vs Blue Team: " .. getGlobalStorageValue(redTeam)
     
----- Tied Score -----            
elseif getGlobalStorageValue(redTeam) == getGlobalStorageValue(blueTeam) then
 str = "Blue Team: "    ..  getGlobalStorageValue(blueTeam) .. " vs Red Team: " .. getGlobalStorageValue(redTeam)
        end
       
----- MSG_EVENT -----                                                          
doBroadcastMessage(str, MESSAGE_EVENT_RED)

----- End -----
return true
end
 
Ideally, you organize the algorithm of your scripts in this way:
LUA:
local blueTeam, redTeam = 101, 102
local scores = {
    [1] = { team = blueTeam },
    [2] = { team = redTeam },
}

----- Init -----
function onThink(interval, lastExecution, thinkInterval)

    ---- Blue Team lead -----
    if getGlobalStorageValue(blueTeam) > getGlobalStorageValue(redTeam) then
        str = "Blue Team: " .. getGlobalStorageValue(blueTeam) .. " vs Red Team: " .. getGlobalStorageValue(redTeam)

        ----- Red Team Lead -----
    elseif getGlobalStorageValue(redTeam) > getGlobalStorageValue(blueTeam) then
        str = "Red Team: " .. getGlobalStorageValue(blueTeam) .. " vs Blue Team: " .. getGlobalStorageValue(redTeam)

        ----- Tied Score -----
    elseif getGlobalStorageValue(redTeam) == getGlobalStorageValue(blueTeam) then
        str = "Blue Team: " .. getGlobalStorageValue(blueTeam) .. " vs Red Team: " .. getGlobalStorageValue(redTeam)
    end

    ----- MSG_EVENT -----
    doBroadcastMessage(str, MESSAGE_EVENT_RED)

    ----- End -----
    return true
end
This will make it easier to read and therefore edit.
Post automatically merged:

You are not using
LUA:
local scores = {
    [1] = { team = blueTeam },
    [2] = { team = redTeam },
}
so, remove it
Post automatically merged:

To improve your script:
LUA:
local blueTeam, redTeam = 101, 102

----- Init -----
function onThink(interval, lastExecution, thinkInterval)
    local getBlue, getRed = getGlobalStorageValue(blueTeam), getGlobalStorageValue(redTeam)
    
    ---- Blue Team lead -----
    if getBlue > getRed then
        str = "Blue Team: " .. getBlue .. " vs Red Team: " .. getRed
        
    ----- Red Team Lead -----
    elseif getBlue < getRed then
        str = "Red Team: " .. getRed .. " vs Blue Team: " .. getBlue

    ----- Tied Score -----
    else
        str = "Both teams have the same score." .. getBlue
    end

    ----- MSG_EVENT -----
    doBroadcastMessage(str, MESSAGE_EVENT_RED)

    ----- End -----
    return true
end
 
Last edited:
Solution
Ideally, you organize the algorithm of your scripts in this way:
LUA:
local blueTeam, redTeam = 101, 102
local scores = {
    [1] = { team = blueTeam },
    [2] = { team = redTeam },
}

----- Init -----
function onThink(interval, lastExecution, thinkInterval)

    ---- Blue Team lead -----
    if getGlobalStorageValue(blueTeam) > getGlobalStorageValue(redTeam) then
        str = "Blue Team: " .. getGlobalStorageValue(blueTeam) .. " vs Red Team: " .. getGlobalStorageValue(redTeam)

        ----- Red Team Lead -----
    elseif getGlobalStorageValue(redTeam) > getGlobalStorageValue(blueTeam) then
        str = "Red Team: " .. getGlobalStorageValue(blueTeam) .. " vs Blue Team: " .. getGlobalStorageValue(redTeam)

        ----- Tied Score -----
    elseif getGlobalStorageValue(redTeam) == getGlobalStorageValue(blueTeam) then
        str = "Blue Team: " .. getGlobalStorageValue(blueTeam) .. " vs Red Team: " .. getGlobalStorageValue(redTeam)
    end

    ----- MSG_EVENT -----
    doBroadcastMessage(str, MESSAGE_EVENT_RED)

    ----- End -----
    return true
end
This will make it easier to read and therefore edit.
Post automatically merged:

You are not using
LUA:
local scores = {
    [1] = { team = blueTeam },
    [2] = { team = redTeam },
}
so, remove it
Post automatically merged:

To improve your script:
LUA:
local blueTeam, redTeam = 101, 102

----- Init -----
function onThink(interval, lastExecution, thinkInterval)
    local getBlue, getRed = getGlobalStorageValue(blueTeam), getGlobalStorageValue(redTeam)
   
    ---- Blue Team lead -----
    if getBlue > getRed then
        str = "Blue Team: " .. getBlue .. " vs Red Team: " .. getRed
       
    ----- Red Team Lead -----
    elseif getBlue < getRed then
        str = "Red Team: " .. getRed .. " vs Blue Team: " .. getBlue

    ----- Tied Score -----
    else
        str = "Both teams have the same score." .. getBlue
    end

    ----- MSG_EVENT -----
    doBroadcastMessage(str, MESSAGE_EVENT_RED)

    ----- End -----
    return true
end
 
LUA:
local scores = {
    blue = 101,
        red = 102,

}
local teams = {
    blueName = "Blue",
        redName = "Red",
}
function onKill(cid)

    if getGlobalStorageValue(scores.blue) > getGlobalStorageValue(scores.red) then
            str = "" ..teams.blueName.. "/n" ..  getGlobalStorageValue(scores.blue) .. " vs" ..teams.redName.. getGlobalStorageValue(scores.red)..""
          
                elseif getGlobalStorageValue(scores.red) > getGlobalStorageValue(scores.blue) then
            str = "" ..teams.redName.. "" ..  getGlobalStorageValue(scores.red) .. " vs " ..teams.blueName.. getGlobalStorageValue(scores.blue)..""
          
          
                        elseif getGlobalStorageValue(scores.red) == getGlobalStorageValue(scores.blue) then 
            str = "" ..teams.blueName.. "" .. getGlobalStorageValue(scores.blue).. " vs " ..teams.redName.. getGlobalStorageValue(scores.red)..""
                                end
                              
                              
                                    doBroadcastMessage(str .. ".", MESSAGE_EVENT_RED)
                                        return true
                                                    end


How can i make this better/faster?! I mean, it's working but, if you make the first kill always shows as "Blue 1 vs -1 Red". Until they even the score the opposite side stays with a negative value.
(even with the globalevents version of it). Anyone can optimize or give me a light? Thanks;
You said it was working so I haven't touched anything I just tidied up the code.
LUA:
local scores = {
    blue = 101,
    red = 102
}

if getGlobalStorageValue(scores.blue) == -1 then
    setGlobalStorageValue(scores.blue, 0)
end

if getGlobalStorageValue(scores.red) == -1 then
    setGlobalStorageValue(scores.red, 0)
end

function onKill(cid, target)
    local blueScore = getGlobalStorageValue(scores.blue)
    local redScore = getGlobalStorageValue(scores.red)
    if blueScore > redScore then
        doBroadcastMessage(string.format("Blue %d vs Red %d", blueScore, redScore), MESSAGE_EVENT_RED)
        return true
    elseif redScore > blueScore then
        doBroadcastMessage(string.format("Red %d vs Blue %d", redScore, blueScore), MESSAGE_EVENT_RED)
        return true
    end

    -- If the scores are equal
    doBroadcastMessage(string.format("Blue %d vs Red %d", blueScore, redScore), MESSAGE_EVENT_RED)
    return true
end
 
Ideally, you organize the algorithm of your scripts in this way:
LUA:
local blueTeam, redTeam = 101, 102
local scores = {
    [1] = { team = blueTeam },
    [2] = { team = redTeam },
}

----- Init -----
function onThink(interval, lastExecution, thinkInterval)

    ---- Blue Team lead -----
    if getGlobalStorageValue(blueTeam) > getGlobalStorageValue(redTeam) then
        str = "Blue Team: " .. getGlobalStorageValue(blueTeam) .. " vs Red Team: " .. getGlobalStorageValue(redTeam)

        ----- Red Team Lead -----
    elseif getGlobalStorageValue(redTeam) > getGlobalStorageValue(blueTeam) then
        str = "Red Team: " .. getGlobalStorageValue(blueTeam) .. " vs Blue Team: " .. getGlobalStorageValue(redTeam)

        ----- Tied Score -----
    elseif getGlobalStorageValue(redTeam) == getGlobalStorageValue(blueTeam) then
        str = "Blue Team: " .. getGlobalStorageValue(blueTeam) .. " vs Red Team: " .. getGlobalStorageValue(redTeam)
    end

    ----- MSG_EVENT -----
    doBroadcastMessage(str, MESSAGE_EVENT_RED)

    ----- End -----
    return true
end
This will make it easier to read and therefore edit.
Post automatically merged:

You are not using
LUA:
local scores = {
    [1] = { team = blueTeam },
    [2] = { team = redTeam },
}
so, remove it
Post automatically merged:

To improve your script:
LUA:
local blueTeam, redTeam = 101, 102

----- Init -----
function onThink(interval, lastExecution, thinkInterval)
    local getBlue, getRed = getGlobalStorageValue(blueTeam), getGlobalStorageValue(redTeam)
   
    ---- Blue Team lead -----
    if getBlue > getRed then
        str = "Blue Team: " .. getBlue .. " vs Red Team: " .. getRed
       
    ----- Red Team Lead -----
    elseif getBlue < getRed then
        str = "Red Team: " .. getRed .. " vs Blue Team: " .. getBlue

    ----- Tied Score -----
    else
        str = "Both teams have the same score." .. getBlue
    end

    ----- MSG_EVENT -----
    doBroadcastMessage(str, MESSAGE_EVENT_RED)

    ----- End -----
    return true
end
Oh. I see!
Ideally, you organize the algorithm of your scripts in this way:
LUA:
local blueTeam, redTeam = 101, 102
local scores = {
    [1] = { team = blueTeam },
    [2] = { team = redTeam },
}

----- Init -----
function onThink(interval, lastExecution, thinkInterval)

    ---- Blue Team lead -----
    if getGlobalStorageValue(blueTeam) > getGlobalStorageValue(redTeam) then
        str = "Blue Team: " .. getGlobalStorageValue(blueTeam) .. " vs Red Team: " .. getGlobalStorageValue(redTeam)

        ----- Red Team Lead -----
    elseif getGlobalStorageValue(redTeam) > getGlobalStorageValue(blueTeam) then
        str = "Red Team: " .. getGlobalStorageValue(blueTeam) .. " vs Blue Team: " .. getGlobalStorageValue(redTeam)

        ----- Tied Score -----
    elseif getGlobalStorageValue(redTeam) == getGlobalStorageValue(blueTeam) then
        str = "Blue Team: " .. getGlobalStorageValue(blueTeam) .. " vs Red Team: " .. getGlobalStorageValue(redTeam)
    end

    ----- MSG_EVENT -----
    doBroadcastMessage(str, MESSAGE_EVENT_RED)

    ----- End -----
    return true
end
This will make it easier to read and therefore edit.
Post automatically merged:

You are not using
LUA:
local scores = {
    [1] = { team = blueTeam },
    [2] = { team = redTeam },
}
so, remove it
Post automatically merged:

To improve your script:
LUA:
local blueTeam, redTeam = 101, 102

----- Init -----
function onThink(interval, lastExecution, thinkInterval)
    local getBlue, getRed = getGlobalStorageValue(blueTeam), getGlobalStorageValue(redTeam)
   
    ---- Blue Team lead -----
    if getBlue > getRed then
        str = "Blue Team: " .. getBlue .. " vs Red Team: " .. getRed
       
    ----- Red Team Lead -----
    elseif getBlue < getRed then
        str = "Red Team: " .. getRed .. " vs Blue Team: " .. getBlue

    ----- Tied Score -----
    else
        str = "Both teams have the same score." .. getBlue
    end

    ----- MSG_EVENT -----
    doBroadcastMessage(str, MESSAGE_EVENT_RED)

    ----- End -----
    return true
end
Oh. I see! Thanks!!!
 
Back
Top