• 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!

Little question about loops :P

Hermes

dziwki kola gramy w lola
Joined
Nov 17, 2007
Messages
1,867
Reaction score
14
Location
Poland
Hi there.

I have one, simple question, to improve my scripting skills.

There's table:
Code:
local table = {
1 = "first option",
2 = "second option",
3 = "third option",
10 = "tenth option"
}

And I want to make a loop for randomizer, so:
with randomizer
Code:
math.random(1,10)
as a first number in table I want script to assign text to those number (ofc number depends on randomizer).

Yeah, I know I have missed some of my LUA lessons with loops xD

Thanks in advance!

Regards,
Hermes
 
First, your table has errors.
Lua:
local t = {
    [1] = "first option",
    [2] = "second option",
    [3] = "third option",
    [10] = "tenth option"
}

Now, we could use this function to get a random value from tables:
Lua:
function table.irand(t)
    -- Function by Colandus
    return t[math.random(1, #t)]
end

This one would only give you 1, 2, 3 and NOT 10. Because just as if you'd have used the loop "for k,v in ipairs(t) do", it would only have showed those 3.

Now, to solve this we could use another function:
Lua:
function table.rand(t)
    -- Function by Colandus
    local tmp = {}
    for _, v in pairs(t) do
        table.insert(tmp, v)
    end
    return table.irand(tmp)
end

Now, you can use table.rand(t) to get a random value from a table.
 
But how to convert this to loop usage?
Lua:
local table = {
[1] = "first text",
[2] = "second text",
[3] = "third text"
)

local randomizer = math.random(1,3)

if randomizer == 1 then
doPlayerSay(cid, "first text")
elseif randomizer == 2 then
doPlayerSay(cid, "second text")
elseif randomizer == 3 then
doPlayerSay(cid, "third text")
end
 
Lua:
local text = ""
chance = math.random(1,3)

if chance = 1 then
 text = "first text"
elseif chance = 2 then
 text = "second text"
elseif chance = 3 then
 text = "third text"
end
   doPlayerSay(cid,text)

I am sure that you can use it so :thumbup:.


EDIT:
Lua:
local table = {
[1] = "first text",
[2] = "second text",
[3] = "thirrd text"
}

local text = table[math.random(1, 3)]
doPlayerSay(cid, text)

This edit not by me.

Regards,
Shawak
 
Yeah, but my method is omfg boring for alot of messages, but second idea is nice, i thought that loop will be better but I failed XD, thanks!
 
Code:
local table = {
[1] = "first text",
[2] = "second text",
[3] = "thirrd text"
}

local text = table[math.random(1, #table)]
doPlayerSay(cid, text)

It`ll automaticaly detect lenght of table. ;P
 
Code:
local table = {
[1] = "first text",
[2] = "second text",
[3] = "thirrd text"
}

local text = table[math.random(1, #table)]
doPlayerSay(cid, text)

It`ll automaticaly detect lenght of table. ;P

PHP:
local t = {
[1] = "first text",
[2] = "second text",
[3] = "thirrd text"
}
local tx = table[math.random(1,#t)]
doPlayerSay(cid,tx)

shorter?
:D

Regards,
Shawak
 
Changing a variable name isn't called to shorten a code. But indeed don't name your variables "table", as it would make you unabled to use table-functions (such as table.insert or w/e).
 
Alindane, the script work, the user is happy, and I ignore you now.

PS: Modarator's please close the theard :).

Regards,
Shawak
 
Back
Top Bottom