Jump to content

Word replacer


holuzs

Recommended Posts

ENG:

So, I try to make a word replacer script, but when I want to replace 2 or more word in a sentence didn't work.

Someone can help me in this? :D

HUN:

Szóval, megpróbáltam csinálni egy "szó helyettesítő scriptet", de ha 2 vagy több szót akarok helyettesíteni, nem működik.

Valaki tudna segíteni? :D

 

An example/Egy példa:

outputChatBox(exports["resourcename"]:replaceWords("%PLAYERNAME% has %PLAYERMONEY% dollar!", "%PLAYERNAME%", getPlayerName(player), "%PLAYERMONEY%", getPlayerMoney(player))

Link to comment
  • Moderators

[HUN ]Szia!

Nem tudom hogy miért így akarod cserélni a szavakat, de itt van egy megoldás a te példádra. (én nem használnám, felesleges export, függvényhívás, loop)

Ebben az esetben a funkció elvárja, hogy a következőképpen kapja meg az argumenteket: replaceWords(EREDETI_SZOVEG, MIT_CSERELJEN, MIRE_CSERELJE, ..., MIT_CSERELJEN, MIRE_CSERELJE, és így tovább...)

function replaceWords(text, ...)
    local args = {...}
    for i=1, #args do
        if (i%2 == 1) then
            text = text:gsub(args[i], args[i+1])
        end
    end
    return text
end
print(replaceWords("#PLAYERNAME has #PLAYERMONEY dollar!", "#PLAYERNAME", "Patrick", "#PLAYERMONEY", 100))

(A % jeleket hanyagolnod kell, mivel az egy helyettesítő karakter. Cseréltem őket #-re.)

Edited by stPatrick
  • Thanks 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...