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

TFS 0.X Random spawn items on the ground function

vexler222

Active Member
Joined
Apr 22, 2012
Messages
714
Solutions
15
Reaction score
46
Hi, i have problem with that function

Code:
function test(cid, item, pos)
  for i = 1, #locations do
    for i = 1, #itemsshop do
            doCreateItem(itemsshop[i].id, 1, locations[i])
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "[Items] " .. itemsshop[i].name .. ".")
  end
  end
  end
return true
end


Cuz i have in "local" ~20 items, but that function, creating every time first 12 items from list, not from random 20 items :(

And second question, its possible to create message with items names all after "," not one by one?

Currently i have:
09:10 [Items] NAME1
09:10 [Items] NAME2
09:10 [Items] NAME3

but i want

09:10 [Items] NAME1, NAME2, NAME3

its possible?
 
Hi, i have problem with that function

Code:
function test(cid, item, pos)
  for i = 1, #locations do
    for i = 1, #itemsshop do
            doCreateItem(itemsshop[i].id, 1, locations[i])
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "[Items] " .. itemsshop[i].name .. ".")
  end
  end
  end
return true
end


Cuz i have in "local" ~20 items, but that function, creating every time first 12 items from list, not from random 20 items :(

And second question, its possible to create message with items names all after "," not one by one?

Currently i have:
09:10 [Items] NAME1
09:10 [Items] NAME2
09:10 [Items] NAME3

but i want

09:10 [Items] NAME1, NAME2, NAME3

its possible?
I don't see the table..?
Did you forget to copy that part?
 
I don't see the table..?
Did you forget to copy that part?

I wrote only function, but table itemsshop look yhat:

local itemsshop = {
{id=2503, name=''Dwarven Armor'},
}

And locations

local locations = {
{x=1024, y=765, z=7, stackpos= 1}
}

I wrote it from memory, cuz currently I'm not on PC :)
 
I wrote only function, but table itemsshop look yhat:

local itemsshop = {
{id=2503, name=''Dwarven Armor'},
}

And locations

local locations = {
{x=1024, y=765, z=7, stackpos= 1}
}

I wrote it from memory, cuz currently I'm not on PC :)
So, loop through every tile, adding 1 random item from the list of possible items, then send a text message with all items spawned to the player who used the lever/item?

Just to make sure this is what you are wanting.
 
So, loop through every tile, adding 1 random item from the list of possible items, then send a text message with all items spawned to the player who used the lever/item?

Just to make sure this is what you are wanting.

I want create random items from list(on list o have 20items) on 12 positions, lever can use only admin in blockade room, that items spawned on the ground only for lottery
 
Sigh.
I'm just going to assume I'm correct since your explanation looks exactly like mine, but more confusing and with information added that's not relevant to the script.

Lua:
local itemsshop = {
    {2503, "Dwarven Armor"}
}

local locations = {
    {x = 1024, y = 765, z = 7}
}

function test(cid)
    local text = "[Items] "
    for i = 1, #locations do
        local rand = math.random(#itemsshop)
        doCreateItem(itemsshop[rand][1], 1, locations[i])
        if text ~= "[Items] " then
            text = text .. ", "
        end
        text = text .. itemsshop[rand][2]
    end
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, text .. ".")
    return true
end
 
Sigh.
I'm just going to assume I'm correct since your explanation looks exactly like mine, but more confusing and with information added that's not relevant to the script.

Lua:
local itemsshop = {
    {2503, "Dwarven Armor"}
}

local locations = {
    {x = 1024, y = 765, z = 7}
}

function test(cid)
    local text = "[Items] "
    for i = 1, #locations do
        local rand = math.random(#itemsshop)
        doCreateItem(itemsshop[rand][1], 1, locations[i])
        if text ~= "[Items] " then
            text = text .. ", "
        end
        text = text .. itemsshop[rand][2]
    end
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, text .. ".")
    return true
end

Not work, items be created, but message dont showed
 
Show full script.

Code:
local itemsshop = {
        {2358, "Crystal Boots"},
        {8983, "PACC Zone Book."},
        {7844, "200 GN Scroll"},
        {12710, "Stamina Doll"},
        {12712, "Retro Doll"},
        {12764, "Bronze Mystery Key"},
        {12718, "Master Upgrade"},
        {12670, "Critical Sword"},
        {12663, "Critical Club"},
        {12662, "Critical Axe"},
        {12661, "Critical Bow"},
        {12664, "Critical Staff"},
        {2501, "Confused Mask"},
        {10570, "Crazy Hat"},
        {2503, "Dwarven Armor"},
        {2504, "Dwarven Legs"}
    }

local locations = {
    {x = 1212, y = 1069, z = 6},
    {x = 1212, y = 1070, z = 6},
    {x = 1212, y = 1071, z = 6},
    {x = 1212, y = 1072, z = 6},
    {x = 1213, y = 1069, z = 6},
    {x = 1213, y = 1070, z = 6},
    {x = 1213, y = 1071, z = 6},
    {x = 1213, y = 1072, z = 6},
    {x = 1214, y = 1069, z = 6},
    {x = 1214, y = 1070, z = 6},
    {x = 1214, y = 1071, z = 6},
    {x = 1214, y = 1072, z = 6}
}

function onUse(cid, item, pos)
    addEvent(msg1, 3000, cid)
    end

function msg1()
            doBroadcastMessage("[Items] Items will create in:", MESSAGE_EVENT_ADVANCE)
                addEvent(msg2, 1000, cid)
                end

function msg2()
            doBroadcastMessage("3", MESSAGE_EVENT_ADVANCE)
                addEvent(msg3, 1000, cid)
                end

function msg3()
            doBroadcastMessage("2", MESSAGE_EVENT_ADVANCE)
                addEvent(msg4, 1000, cid)
                end

function msg4()
            doBroadcastMessage("1", MESSAGE_EVENT_ADVANCE)
            addEvent(test, 1000, cid)
            end

function test(cid)
    local text = "[Items] "
    for i = 1, #locations do
        local rand = math.random(#itemsshop)
        doCreateItem(itemsshop[rand][1], 1, locations[i])
        if text ~= "[Items] " then
            text = text .. ", "
        end
        text = text .. itemsshop[rand][2]
    end
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, text .. ".")
    return true
end
 
@vexler222
function msg1() change to function msg1(cid) AND function msg2() change to function msg2(cid) etc because you pass parameter to function which doesn't accept any and because of that parameter is lost

btw. this message will only receive person which used item

i think what you need is onTime() event in globalevents
 
onTime is event assigned to globalevents I think. onTime event is usally used as automatic server event starter

it can be used like this

event.lua
Lua:
function onTime()
    local msgType = 22
    local messages = {
        "blabla",
        "blablablabla"
    }

    doBroadcastMessage(messages[math.random(1,table.maxn(messages))], msgType)
    return TRUE
end

globalevents.xml
XML:
<globalevent name="event1" time="7:00" event="script" value="event.lua"/>

this "event" will randomly broadcast message from messages table at 7:00 everyday
 
onTime is event assigned to globalevents I think. onTime event is usally used as automatic server event starter

it can be used like this

event.lua
Lua:
function onTime()
    local msgType = 22
    local messages = {
        "blabla",
        "blablablabla"
    }

    doBroadcastMessage(messages[math.random(1,table.maxn(messages))], msgType)
    return TRUE
end

globalevents.xml
XML:
<globalevent name="event1" time="7:00" event="script" value="event.lua"/>

this "event" will randomly broadcast message from messages table at 7:00 everyday


But how spawn items in globalevents?
 
Like in actions,moveevents,creaturescript etc. by dedicated function "doCreateItem(itemid, type/count, pos)"

check this
Lua:
local items = {
    {
        id = 2160,
        count = 100
    },
    {
        id = 2333,
        count = 1
    },
    {
        id = 3111,
        count = 1
    }
}

local positions = {
    from = {x=123, y=321, z=7},
    to = {x= 129, y=340, z=7}
}

local times = 14 -- how many times script will create items

function onTime()
    msg1()
    return true
end



function randomSpawn(items, positions, times)
    local item = items[math.random(1,#items)]
    local pos = {
        x=math.random(positions.from.x, positions.to.x),
        y=math.random(positions.from.y, positions.to.y),
        z=math.random(positions.from.z, positions.to.z)
    }

    doCreateItem(item.id, item.count, pos)
    doBroadcastMessage("[EVENT] " .. getItemNameById(item.id) .. " has been spawned", 22)

    if times > 0 then
        addEvent(randomSpawn, 5000, items,positions,times-1)
    else
        return true
    end
end

function msg1()
    doBroadcastMessage("[EVENT] Raining Items Event will start in:", MESSAGE_EVENT_ADVANCE)
    addEvent(msg2, 1000)
end

function msg2()
    doBroadcastMessage("3", MESSAGE_EVENT_ADVANCE)
    addEvent(msg3, 1000)
end

function msg3()
    doBroadcastMessage("2", MESSAGE_EVENT_ADVANCE)
    addEvent(msg4, 1000)
end

function msg4()
    doBroadcastMessage("1", MESSAGE_EVENT_ADVANCE)
    addEvent(randomSpawn, 1000, items, positions, times)
end

@edit
@vexler222 check this code, now it's working properly
 
Last edited:
Like in actions,moveevents,creaturescript etc. by dedicated function "doCreateItem(itemid, type/count, pos)"

check this
Lua:
local items = {
    {
        id = 2160,
        count = 100
    },
    {
        id = 2333,
        count = 1
    },
    {
        id = 3111,
        count = 1
    }
}

local positions = {
    from = {x=123, y=321, z=7}
    to = {x= 129, y=340, z=7}
}

local times = 14 -- how many times script will create items

function onTime()
    msg1()
    return true
end



function randomSpawn(items, positions, times)
    local item = items[math.random(1,#items)]
    local pos = {
        x=math.random(positions.from.x, positions.to.x),
        y=math.random(positions.from.y, positions.to.y),
        z=math.random(positions.from.z, positions.to.z)
    }

    doCreateItem(item.id, item.count, pos)
    doBroadcastMessage("[EVENT] " . getItemNameById(item.id) . " has been spawned", 22)

    if times > 0 then
        addEvent(randomSpawn, 5000, items,positions,times-1)
    else
        return true
    end
end

function msg1()
    doBroadcastMessage("[EVENT] Raining Items Event will start in:", MESSAGE_EVENT_ADVANCE)
    addEvent(msg2, 1000)
end

function msg2()
    doBroadcastMessage("3", MESSAGE_EVENT_ADVANCE)
    addEvent(msg3, 1000)
end

function msg3()
    doBroadcastMessage("2", MESSAGE_EVENT_ADVANCE)
    addEvent(msg4, 1000)
end

function msg4()
    doBroadcastMessage("1", MESSAGE_EVENT_ADVANCE)
    addEvent(randomSpawn, 1000, items, positions, times)
end

Thanks, but your script creating same items on random pos, but i want random items on same pos :D
But thanks, i will try edit your script.
 
Back
Top