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

Lua create function shuffle(numbers)

beenii

Well-Known Member
Joined
Jul 26, 2010
Messages
580
Solutions
1
Reaction score
57
Hello I think that interesting systems could be created with a function like this
I need it for a system to change townid of my players

the truth is, I don't know how to store data in tables, to create this function

the idea of the function is the following:

be able to use the function: shuffle(numbers) and place the numbers that we need to shuffle for example:

run shuffle(1,2,3,4,5)

and to be able to individually consult the order of each number

result:
shuffle.[1]=5
shuffle.[2]=2
shuffle.[3]=1
shuffle.[4]=3
shuffle.[5]=4

use again
shuffle(1,2,3,4,5,6) now with another extra number
shuffle.[1]=2
shuffle.[2]=4
shuffle.[3]=6
shuffle.[4]=5
shuffle.[5]=1
shuffle.[6]=3

obtaining the numbers in a different order, and this order is preserved until we execute the function shuffle again


It does not matter if we store the order in global storage or in mysql, or in the same script, it would help me anyway, I hope can help me
 
Solution
This is pretty easy to do tho?

Idk why you'd need a dedicated function for it, but here you go.

function
Lua:
function shuffle(array)
    local shuffledArray = {}
    while #array > 0 do
        local rand = math.random(#array)
        table.insert(shuffledArray, array[rand])
        table.remove(array, rand)
    end
    return shuffledArray
end
Usage
Lua:
local shuffle = shuffle({1,2,3,4,5,6,7,8,9})
for i = 1, #shuffle do
    print(shuffle[i])
end
Lua:
local shuffle = shuffle({"dog","cat","elephant","wolf","whale"})
for i = 1, #shuffle do
    print(shuffle[i])
end
the truth is, I don't know how to store data in tables, to create this function
You can just learn how to do it. There is no sense for making your function in tfs repo because it's senseless. You can change townid by executing simple sql query without any external shuffle
 
You can just learn how to do it. There is no sense for making your function in tfs repo because it's senseless. You can change townid by executing simple sql query without any external shuffle
It is a function in Lua you do not have to change anything from the repository
 
This is pretty easy to do tho?

Idk why you'd need a dedicated function for it, but here you go.

function
Lua:
function shuffle(array)
    local shuffledArray = {}
    while #array > 0 do
        local rand = math.random(#array)
        table.insert(shuffledArray, array[rand])
        table.remove(array, rand)
    end
    return shuffledArray
end
Usage
Lua:
local shuffle = shuffle({1,2,3,4,5,6,7,8,9})
for i = 1, #shuffle do
    print(shuffle[i])
end
Lua:
local shuffle = shuffle({"dog","cat","elephant","wolf","whale"})
for i = 1, #shuffle do
    print(shuffle[i])
end
 
Solution
This is pretty easy to do tho?

Idk why you'd need a dedicated function for it, but here you go.

function
Lua:
function shuffle(array)
    local shuffledArray = {}
    while #array > 0 do
        local rand = math.random(#array)
        table.insert(shuffledArray, array[rand])
        table.remove(array, rand)
    end
    return shuffledArray
end
Usage
Lua:
local shuffle = shuffle({1,2,3,4,5,6,7,8,9})
for i = 1, #shuffle do
    print(shuffle[i])
end
Lua:
local shuffle = shuffle({"dog","cat","elephant","wolf","whale"})
for i = 1, #shuffle do
    print(shuffle[i])
end

It works perfectly 😍

a question, when i run
Lua:
local shuffle = shuffle({1,2,3,4,5,6,7,8,9})
for i = 1, #shuffle do
    print(shuffle[i])
end

To check the order after executing, i need to store them in a global storage right?

The reason why I needed this function I am making a war system where I have map change every hours, and I want the map to be random, but not repeat the same map.
 
It works perfectly 😍

a question, when i run
Lua:
local shuffle = shuffle({1,2,3,4,5,6,7,8,9})
for i = 1, #shuffle do
    print(shuffle[i])
end

To check the order after executing, i need to store them in a global storage right?

The reason why I needed this function I am making a war system where I have map change every hours, and I want the map to be random, but not repeat the same map.
You can store them in a global table, the caveat being that the table will be destroyed upon server reset.
If you need the information to persist between server saves, then yeah, globalstorage, or a text document.
 
You can store them in a global table, the caveat being that the table will be destroyed upon server reset.
If you need the information to persist between server saves, then yeah, globalstorage, or a text document.

can you teach me how to store in a global table?
it doesn't matter that it gets destroyed when the server restarts
 
can you teach me how to store in a global table?
it doesn't matter that it gets destroyed when the server restarts
I'll have to show you over discord I think.
It's dependent on your tfs & script.
 
Back
Top