• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

problem in file:read function (getWandDmg/getWandType)

beenii

Well-Known Member
Joined
Jul 26, 2010
Messages
586
Solutions
1
Reaction score
58
Code:
function getWandDmg(itemId)
local file = io.open("data/weapons/weapons.xml", "r")
    for info in string.gmatch(file:read("*a"), "<weapons(.-)</weapons>") do
        if info:match("wand id=\"(.-)\"") == itemId then
            local min = info:match("min=(.-)")
            local max = info:match("max=(.-)")
            return math.random(min, max)
        end
    end
    file:close()
    return nil
end

function getWandType(itemId)
local file = io.open("data/weapons/weapons.xml", "r")
    for info in string.gmatch(file:read("*a"), "<weapons(.-)</weapons>") do
        if info:match("wand id=\"(.-)\"") == itemId then
            local type = info:match("type=(.-)")
            return tostring(type)
        end
    end
    file:close()
    return nil
end

i try use this function. but in console error of a nil value

i use tfs 0.4
 
Code:
function getWandDmg(itemId)
    local file = io.open("data/weapons/weapons.xml", "r")
    for info in string.gmatch(file:read("*a"), "<weapons(.-)</weapons>") do
        if tonumber(info:match("wand id=\"(.-)\"")) == itemId then
            local min = tonumber(info:match('min="(.-)"'))
            local max = tonumber(info:match('max="(.-)"'))
            return math.random(min, max)
        end

    end
    file:close()
    return nil
end

function getWandType(itemId)
    local file = io.open("data/weapons/weapons.xml", "r")
    for info in string.gmatch(file:read("*a"), "<weapons(.-)</weapons>") do
        if tonumber(info:match('wand id="(.-)"')) == itemId then
            local type = info:match('type="(.-)"')
            return tostring(type)
        end
    end
    file:close()
    return nil
end
 
Code:
function getWandDmg(itemId)
    local file = io.open("data/weapons/weapons.xml", "r")
    for info in string.gmatch(file:read("*a"), "<weapons(.-)</weapons>") do
        if tonumber(info:match("wand id=\"(.-)\"")) == itemId then
            local min = tonumber(info:match('min="(.-)"'))
            local max = tonumber(info:match('max="(.-)"'))
            return math.random(min, max)
        end

    end
    file:close()
    return nil
end

function getWandType(itemId)
    local file = io.open("data/weapons/weapons.xml", "r")
    for info in string.gmatch(file:read("*a"), "<weapons(.-)</weapons>") do
        if tonumber(info:match('wand id="(.-)"')) == itemId then
            local type = info:match('type="(.-)"')
            return tostring(type)
        end
    end
    file:close()
    return nil
end


omg u are the best :D

I like to check that I had bad.
I see tonumber missing. to seek the id of the wand.
thank you so much.
 
Code:
function getWandDmg(itemId)
    local file = io.open("data/weapons/weapons.xml", "r")
    for info in string.gmatch(file:read("*a"), "<weapons(.-)</weapons>") do
        if tonumber(info:match("wand id=\"(.-)\"")) == itemId then
            local min = tonumber(info:match('min="(.-)"'))
            local max = tonumber(info:match('max="(.-)"'))
            return math.random(min, max)
        end

    end
    file:close()
    return nil
end

function getWandType(itemId)
    local file = io.open("data/weapons/weapons.xml", "r")
    for info in string.gmatch(file:read("*a"), "<weapons(.-)</weapons>") do
        if tonumber(info:match('wand id="(.-)"')) == itemId then
            local type = info:match('type="(.-)"')
            return tostring(type)
        end
    end
    file:close()
    return nil
end


small bug xD only return valor first wand in weapons.xml :O
 
Code:
function getWandDmg(itemId)
    local file = "data/weapons/weapons.xml"
    for info in io.lines(file) do
        for _, id in info:gmatch('<(%a-)%s* id="(.-)"') do
            if tonumber(id) == itemId then
                local min = tonumber(info:match('min="(.-)"'))
                local max = tonumber(info:match('max="(.-)"'))
                return math.random(min, max)
            end
        end
    end
    return nil
end

function getWandType(itemId)
    local file = "data/weapons/weapons.xml"
    for info in io.lines(file) do
        for _, id in info:gmatch('<(%a-)%s* id="(.-)"') do
            if tonumber(id) == itemId then
                local type = info:match('type="(.-)"')
                return tostring(type)
            end
        end
    end
    return nil
end
 
Code:
function getWandDmg(itemId)
    local file = "data/weapons/weapons.xml"
    for info in io.lines(file) do
        for _, id in info:gmatch('<(%a-)%s* id="(.-)"') do
            if tonumber(id) == itemId then
                local min = tonumber(info:match('min="(.-)"'))
                local max = tonumber(info:match('max="(.-)"'))
                return math.random(min, max)
            end
        end
    end
    return nil
end

function getWandType(itemId)
    local file = "data/weapons/weapons.xml"
    for info in io.lines(file) do
        for _, id in info:gmatch('<(%a-)%s* id="(.-)"') do
            if tonumber(id) == itemId then
                local type = info:match('type="(.-)"')
                return tostring(type)
            end
        end
    end
    return nil
end

Working perfect bro! thanks thanks! :D
 
Back
Top