• 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 Random results

Kuantikum

Member
Joined
Jul 3, 2015
Messages
219
Solutions
1
Reaction score
20
local exemple = table.random{"A", "B", "C", "D", "E"}

Hello guys!

Im trying to get a random value from "exemple ". It give me a random value, but when ill repeat the function again it give me the same value and it just give me a different one when i restart the server.

Exemple: The firs try it give me random A, when i try the function again, it give me again the A. When i restar the server it give me B or other random one with each restart.

How can i set it to give me random results each time i execute the function ?

Thxx!
 
Lua:
local example = {"A", "B", "C", "D", "E"}
local randomRes = example[math.random(1, #example)]
 
Seems like you are using/storing math.random value outside and not inside the function body
 
Last edited:
Lua:
local example = {"A", "B", "C", "D", "E"}
local randomRes = example[math.random(1, #example)]

I get this error:

Lua:
[Error - LuaScriptInterface::loadFile] data/lib/game_dungeon_maps.lua:142: unexpected symbol near '['
Post automatically merged:

Seems like you are using/storing math.random value outside and not inside the function body
Im trying to do this in LIB and then other LIB archive call this results
 
Last edited:
Can you show us how are you doing it? If you are just calling math.random in a global environment, thats the reason
Yes, sure!

Im trying to get a random result from this archive1.lua in LIB to archive2.lua in the same LIB directory.

at Archive1.lua there is where the info are :D


Lua:
local randomMonsters = table.random{"Terminator", "Iron Man", "Batman", "Pussycat Woman", "Lotus"}
local randomRes = [math.random(1, #randomMonsters)]

Dz.Diff[DzBeginner].Maps[1].rooms[1] = {
  pos = {player = {x = 4670, y = 429, z = 7}, from = {x = 4662, y = 389, z = 4}, to = {x = 4739, y = 469, z = 7}},
  inUse = false,
  variable = {monstersCount = 0, time = nil, members = {}, monsters = {}},
  monsters = {
 
 {name = ""..randomRes.."", pos = {x = 4679, y = 435, z = 7}




Archive2.lua im try to execute a random monsters to random positions and if is walls, water, rocks (something block the area) so it check free space and create the mosnters there.


Lua:
for _, monsters in ipairs(Room.monsters) do
    local pid = doCreateMonster(monsters.name, monsters.pos, false)
    if isCreature(pid) then table.insert(Room.variable.monsters, pid) end
  end
Post automatically merged:

Yes, sure!

Im trying to get a random result from this archive1.lua in LIB to archive2.lua in the same LIB directory.

at Archive1.lua there is where the info are :D


Lua:
local randomMonsters = table.random{"Terminator", "Iron Man", "Batman", "Pussycat Woman", "Lotus"}
local randomRes = [math.random(1, #randomMonsters)]

Dz.Diff[DzBeginner].Maps[1].rooms[1] = {
  pos = {player = {x = 4670, y = 429, z = 7}, from = {x = 4662, y = 389, z = 4}, to = {x = 4739, y = 469, z = 7}},
  inUse = false,
  variable = {monstersCount = 0, time = nil, members = {}, monsters = {}},
  monsters = {
 
 {name = ""..randomRes.."", pos = {x = 4679, y = 435, z = 7}




Archive2.lua im try to execute a random monsters to random positions and if is walls, water, rocks (something block the area) so it check free space and create the mosnters there.


Lua:
for _, monsters in ipairs(Room.monsters) do
    local pid = doCreateMonster(monsters.name, monsters.pos, false)
    if isCreature(pid) then table.insert(Room.variable.monsters, pid) end
  end
I edited the order XD
Post automatically merged:

Yes, sure!

Im trying to get a random result from this archive1.lua in LIB to archive2.lua in the same LIB directory.

at Archive1.lua there is where the info are :D


Lua:
local randomMonsters = table.random{"Terminator", "Iron Man", "Batman", "Pussycat Woman", "Lotus"}
local randomRes = [math.random(1, #randomMonsters)]

Dz.Diff[DzBeginner].Maps[1].rooms[1] = {
  pos = {player = {x = 4670, y = 429, z = 7}, from = {x = 4662, y = 389, z = 4}, to = {x = 4739, y = 469, z = 7}},
  inUse = false,
  variable = {monstersCount = 0, time = nil, members = {}, monsters = {}},
  monsters = {
 
 {name = ""..randomRes.."", pos = {x = 4679, y = 435, z = 7}




Archive2.lua im try to execute a random monsters to random positions and if is walls, water, rocks (something block the area) so it check free space and create the mosnters there.


Lua:
for _, monsters in ipairs(Room.monsters) do
    local pid = doCreateMonster(monsters.name, monsters.pos, false)
    if isCreature(pid) then table.insert(Room.variable.monsters, pid) end
  end
Post automatically merged:


I edited the order XD


PS: I tryed to add something like that, but i dont know how to add it:

Lua:
local function isPathable(cid, pos)
    if getTileThingByPos(pos).itemid ~= 0 then
        local tile = getThingfromPos(pos).uid   
        if hasProperty(tile, CONST_PROP_BLOCKSOLID) then
            return false
        end
        return true
    end
    local pos = {x = 0, y = 0, z = 0, stackpos = 0}
     while continue == false do
            pos.x = math.random(top_left_corner.x, bottom_right_corner.x)
            pos.y = math.random(top_left_corner.y, bottom_right_corner.y)
            pos.z = math.random(top_left_corner.z, bottom_right_corner.z)
            if not isInArray(water_tiles, getTileThingByPos(pos).itemid) then
                if isPathable(pos) then
                    continue = true
                end
            end
        end
        continue = false
    return false
end
 
Last edited:
Im trying to get a random result from this archive1.lua in LIB to archive2.lua in the same LIB directory.

Lua:
Dz.Diff[DzBeginner].Maps[1].rooms[1] = {
      pos = {player = {x = 4670, y = 429, z = 7}, from = {x = 4662, y = 389, z = 4}, to = {x = 4739, y = 469, z = 7}},
      inUse = false,
      variable = {monstersCount = 0, time = nil, members = {}, monsters = {}},

      -- NEW CODE
      monsters = {"Terminator", "Iron Man", "Batman", "Pussycat Woman", "Lotus"},
      monsterPositions = {
         [1] = {x = 4679, y = 435, z = 7},
         [2] = {x = 4679, y = 435, z = 7},
         [3] = {x = 4679, y = 435, z = 7},
    }
}

-- code inside function
for i = 1, #Room.monsterPositions do
    -- before you summon the monster you can check if the position is walkable
    -- but since postions are being set manually, i suppose its not necessary?
    local position = Room.monsterPositions[i]
    local pid = doCreateMonster(Room.monsters[math.random(1, #Room.monsters)], position, false, true)

    if isCreature(pid) then
        table.insert(Room.variable.monsters, pid)
    end
end
 
Lua:
Dz.Diff[DzBeginner].Maps[1].rooms[1] = {
      pos = {player = {x = 4670, y = 429, z = 7}, from = {x = 4662, y = 389, z = 4}, to = {x = 4739, y = 469, z = 7}},
      inUse = false,
      variable = {monstersCount = 0, time = nil, members = {}, monsters = {}},

      -- NEW CODE
      monsters = {"Terminator", "Iron Man", "Batman", "Pussycat Woman", "Lotus"},
      monsterPositions = {
         [1] = {x = 4679, y = 435, z = 7},
         [2] = {x = 4679, y = 435, z = 7},
         [3] = {x = 4679, y = 435, z = 7},
    }
}

-- code inside function
for i = 1, #Room.monsterPositions do
    -- before you summon the monster you can check if the position is walkable
    -- but since postions are being set manually, i suppose its not necessary?
    local position = Room.monsterPositions[i]
    local pid = doCreateMonster(Room.monsters[math.random(1, #Room.monsters)], position, false, true)

    if isCreature(pid) then
        table.insert(Room.variable.monsters, pid)
    end
end

OMGAH!!
LOVE YOU XD

Lua:
-- but since positions are being set manually, i suppose its not necessary?

The idea is like a dungeon, so it will be around 100+ of them and in the dungeon will be 333+ monsters XD, so set manual i think ill never ends XD.

I have understood what you do but i dont know how to turn it on random position FROM POS TO POS like:

Lua:
from = {x = 4662, y = 389, z = 4}, to = {x = 4739, y = 469, z = 7}
 
Lua:
Dz.Diff[DzBeginner].Maps[1].rooms[1] = {
      pos = {player = {x = 4670, y = 429, z = 7}, from = {x = 4662, y = 389, z = 4}, to = {x = 4739, y = 469, z = 7}},
      inUse = false,
      variable = {monstersCount = 0, time = nil, members = {}, monsters = {}},

      -- NEW CODE
      monsters = {"Terminator", "Iron Man", "Batman", "Pussycat Woman", "Lotus"},
      monsterPositions = {
         [1] = {x = 4679, y = 435, z = 7},
         [2] = {x = 4679, y = 435, z = 7},
         [3] = {x = 4679, y = 435, z = 7},
    }
}

-- code inside function
for i = 1, #Room.monsterPositions do
    -- before you summon the monster you can check if the position is walkable
    -- but since postions are being set manually, i suppose its not necessary?
    local position = Room.monsterPositions[i]
    local pid = doCreateMonster(Room.monsters[math.random(1, #Room.monsters)], position, false, true)

    if isCreature(pid) then
        table.insert(Room.variable.monsters, pid)
    end
end

Great ty!



I get some erros and i change again to this:

Lua:
local randomMonsters= {"Terminator", "Iron Man", "Batman", "Pussycat Woman", "Lotus"}

Dz.Diff[DzBeginner].Maps[1].rooms[1] = {
  pos = {player = {x = 4670, y = 429, z = 7}, from = {x = 4662, y = 389, z = 4}, to = {x = 4739, y = 469, z = 7}},
  inUse = false,
  variable = {monsterCount = 0, time = nil, members = {}, monsters = {}},
  monsters = {
    {name = randomMonsters, pos = {from = {x = 4662, y = 389, z = 4}, to = {x = 4739, y = 469, z = 7}},
  
  }


And in the function i back to some like this:

Lua:
local position = {math.random(Room.monsters.pos)}
 
  for _, monster in ipairs(Room.monsters) do
    local pid = doCreateMonster(monster.name[math.random(1, #monster.name)], position, false)
    if isCreature(pid) then table.insert(Room.variable.monsters, pid) end
  end


It works fine with a single pos, but im trying to get a random pos and i get this error back:

1665184160101.png

Line:
1665184111609.png

Can you help me Thx!!!
 
Back
Top