• 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 Collection NPC #tables.

OTcreator

Well-Known Member
Joined
Feb 14, 2022
Messages
503
Solutions
1
Reaction score
57
Hi,
I tried to write a script where we will have to give a random amount of items to an NPC from a table.
In return, we will also receive random rewards.

But something doesn't work for me here, it doesn't work. Error in the console regarding the table.
I have never worked on such tables hence I assisted with GPT.

In the next steps, I was to add rewards and the possibility of cancelling a given task and a new one could be started only after an hour.
Code for TFS 1.4.

LUA:
local missions = {
    {itemId = 2160, count = math.random(1, 10)},
    {itemId = 2152, count = math.random(1, 10)},
    {itemId = 2148, count = math.random(1, 10)},
    {itemId = 7590, count = math.random(1, 10)},
    {itemId = 7412, count = math.random(1, 10)},
}

local currentMission = nil

function onCreatureSay(cid, type, msg)
    if msg:lower() == "mission" then
        if currentMission == nil or getPlayerStorageValue(cid, 10000) >= 1 then
            currentMission = missions[math.random(#missions)]
            setPlayerStorageValue(cid, 10000, 0)
            doPlayerSendTextMessage(cid, MESSAGE_EVENT_DEFAULT, "Mission: Bring me " .. currentMission.count .. "x " .. getItemName(currentMission.itemId) .. ".")
        else
            doPlayerSendTextMessage(cid, MESSAGE_EVENT_DEFAULT, "You have already mission. Bring me " .. currentMission.count .. "x " .. getItemName(currentMission.itemId) .. ".")
        end
    end

    if msg:lower() == "done" then
        if currentMission and getPlayerItemCount(cid, currentMission.itemId) >= currentMission.count then
            doPlayerRemoveItem(cid, currentMission.itemId, currentMission.count)
            doPlayerSendTextMessage(cid, MESSAGE_EVENT_DEFAULT, "Thank you! Already have " .. currentMission.count .. "x " .. getItemName(currentMission.itemId) .. "!")
            setPlayerStorageValue(cid, 10000, 1)
            currentMission = nil
        else
            doPlayerSendTextMessage(cid, MESSAGE_EVENT_DEFAULT, "You don't have items.")
        end
    end
end

local npcHandler = NpcHandler:new()
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, onCreatureSay)
npcHandler:setCallback(CALLBACK_ONCHANGEOUTFIT, onCreatureChangeOutfit)
npcHandler:addModule(FocusModule:new())
 
Hard to say what is wrong with this script. Everything is wrong :(

1. This code is outside onCreatureSay, so it's not random count per player. It's random per server restart (when .lua is loaded) with same count for all players until next server restart.
LUA:
local missions = {
    {itemId = 2160, count = math.random(1, 10)},
    {itemId = 2152, count = math.random(1, 10)},
    {itemId = 2148, count = math.random(1, 10)},
    {itemId = 7590, count = math.random(1, 10)},
    {itemId = 7412, count = math.random(1, 10)},
}
2. Players missions are totally wrong.
local currentMission = nil - it's 1 variable for all players on OTS. It's also reset every server restart. You can't store selected mission like that
getPlayerStorageValue(cid, 10000) >= 1 - where is it set to 1 for first time? How player can start mission for first time (default storage is -1)?
currentMission = missions[math.random(#missions)] - you pick random mission, but does not store it anywhere, it should be stored in player storage
LUA:
local currentMission = nil

function onCreatureSay(cid, type, msg)
    if msg:lower() == "mission" then
        if currentMission == nil or getPlayerStorageValue(cid, 10000) >= 1 then
            currentMission = missions[math.random(#missions)]
            setPlayerStorageValue(cid, 10000, 0)

It should be something like:
LUA:
if getPlayerStorageValue(cid, 10000) < 1 then
-- player has no mission
-- set it to random mission
  local playerMissionId = math.random(#missions)
  setPlayerStorageValue(cid, 10000, playerMissionId)
-- save player items required count
  local playerItemCount = math.random(10)
  setPlayerStorageValue(cid, 10001, playerItemCount)

  local playerMission = missions[playerMissionId]
-- you can use playerMission to read item id/mission name etc. from config
-- send message about assigned mission
else
-- player has mission
  local playerMission = missions[getPlayerStorageValue(cid, 10000)]
  local playerItemCount = getPlayerStorageValue(cid, 10001)
-- here you can check if player collected required item count and send message about progress/give rewards
end
 
Back
Top