• 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 math.random teleport

Kahras

Member
Joined
Aug 6, 2012
Messages
101
Reaction score
7
Location
Warsaw
Lua:
local config = {
    playerPositions = {
        Position(736, 929, 8),
        Position(737, 929, 8),
        Position(738, 929, 8)
    },
    
    newPositions = {
        Position(748, 933, 8),
        Position(750, 933, 8),
        Position(732, 929, 8)
    }
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if item.itemid == 1946 then
        local storePlayers, playerTile = {}
        for i = 1, #config.playerPositions do
            playerTile = Tile(config.playerPositions[i]):getTopCreature()
            if not playerTile or not playerTile:isPlayer() then
                player:sendTextMessage(MESSAGE_STATUS_SMALL, "You need 3 players.")
                return true
            end
            storePlayers[#storePlayers + 1] = playerTile
        end    
    local players
        for i = 1, #storePlayers do
            local randomPosition = config.newPositions[math.random(#config.newPositions)]
            players = storePlayers[i]
            config.playerPositions[i]:sendMagicEffect(CONST_ME_POFF)
            players:teleportTo(randomPosition)
            players:setDirection(EAST)
        
    end
    
    end

    item:transform(item.itemid == 1946 and 1945 or 1946)
    return true
end



How to make everyone teleported to 1 random place?
Because now it happens that 2 players teleport to the same place.
 
Last edited:
Solution
B
When you are getting the random new position, you need to remove it from the table, otherwise the next time you get a random position, it can pick the same one, hence why 2 players are sometimes going to the same tile...

Untested but should work:
Lua:
local config = {
    playerPositions = {
        Position(736, 929, 8),
        Position(737, 929, 8),
        Position(738, 929, 8)
    },
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if item.itemid == 1946 then
        local newPositions = {
            Position(748, 933, 8),
            Position(750, 933, 8),
            Position(732, 929, 8)
        }
        local storePlayers, playerTile = {}
      
        for i = 1, #config.playerPositions do...
You only have 3 possible positions, it is very likely that they all players end up in the same position, what are you trying to do with this script? If you don't want them to be in the same position, why use randomness?
 
He wants each player to teleport to a different position. Now it happens that 2 players teleport to the same place.

JlZqK3P.png
 
Last edited:
I assume you want to randomise which players go to each tile. All you need to do is shuffle the storePlayers table before you loop through it.

If you dont want to restructure your code and keep it the same way you've done it. You just need to remove that index once you've randomly chosen it.
 
I assume you want to randomise which players go to each tile. All you need to do is shuffle the storePlayers table before you loop through it.

If you dont want to restructure your code and keep it the same way you've done it. You just need to remove that index once you've randomly chosen it.
I don't understand
 
When you are getting the random new position, you need to remove it from the table, otherwise the next time you get a random position, it can pick the same one, hence why 2 players are sometimes going to the same tile...

Untested but should work:
Lua:
local config = {
    playerPositions = {
        Position(736, 929, 8),
        Position(737, 929, 8),
        Position(738, 929, 8)
    },
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if item.itemid == 1946 then
        local newPositions = {
            Position(748, 933, 8),
            Position(750, 933, 8),
            Position(732, 929, 8)
        }
        local storePlayers, playerTile = {}
      
        for i = 1, #config.playerPositions do
            playerTile = Tile(config.playerPositions[i]):getTopCreature()
            if not playerTile or not playerTile:isPlayer() then
                player:sendTextMessage(MESSAGE_STATUS_SMALL, "You need 3 players.")
                return true
            end
            storePlayers[#storePlayers + 1] = playerTile
        end
      
        for i = 1, #storePlayers do
            local rand = math.random(#newPositions)
            local randomPosition = newPositions[rand]
          
            local p = storePlayers[i]
            config.playerPositions[i]:sendMagicEffect(CONST_ME_POFF)
            p:teleportTo(randomPosition)
            p:setDirection(EAST)    

            table.remove(newPositions, rand)
        end
      
    end
  
    item:transform(item.itemid == 1946 and 1945 or 1946)
    return true
end
 
Solution
When you are getting the random new position, you need to remove it from the table, otherwise the next time you get a random position, it can pick the same one, hence why 2 players are sometimes going to the same tile...

Untested but should work:
Lua:
local config = {
    playerPositions = {
        Position(736, 929, 8),
        Position(737, 929, 8),
        Position(738, 929, 8)
    },
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if item.itemid == 1946 then
        local newPositions = {
            Position(748, 933, 8),
            Position(750, 933, 8),
            Position(732, 929, 8)
        }
        local storePlayers, playerTile = {}
     
        for i = 1, #config.playerPositions do
            playerTile = Tile(config.playerPositions[i]):getTopCreature()
            if not playerTile or not playerTile:isPlayer() then
                player:sendTextMessage(MESSAGE_STATUS_SMALL, "You need 3 players.")
                return true
            end
            storePlayers[#storePlayers + 1] = playerTile
        end
     
        for i = 1, #storePlayers do
            local rand = math.random(#newPositions)
            local randomPosition = newPositions[rand]
         
            local p = storePlayers[i]
            config.playerPositions[i]:sendMagicEffect(CONST_ME_POFF)
            p:teleportTo(randomPosition)
            p:setDirection(EAST)   

            table.remove(newPositions, rand)
        end
     
    end
 
    item:transform(item.itemid == 1946 and 1945 or 1946)
    return true
end
okay now i get it!
Thank you very much ;)
 
Back
Top