• 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 Problem with cid variables?

infrator123

New Member
Joined
Aug 10, 2011
Messages
24
Reaction score
2
Theres any problem using alot of differents cid variables in npcs like this? this way i think its secure to control everything in big scripts..


local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

local talkState = {}
local moneyPrice, destination, cityName = {}, {}, {} <<<<<<<<<<<< here

function onCreatureAppear(cid) npcHandler:eek:nCreatureAppear(cid) end
function onCreatureDisappear(cid) npcHandler:eek:nCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg) npcHandler:eek:nCreatureSay(cid, type, msg) end
function onThink() npcHandler:eek:nThink() end

function greet(cid)
moneyPrice[cid], destination[cid], cityName[cid] = nil, nil, nil <<<<<<<<< here
return true
end


function creatureSayCallback(cid, type, msg)
if(not npcHandler:isFocused(cid)) then
return false
end
local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
if (msgcontains(msg, 'somewhere')) or (msgcontains(msg, 'carlin')) then
cityName[cid] = "Somewhere" <<<<<<< here
destination[cid] = {x = 4495, y = 3627, z = 7} <<<<<<< here
selfSay("The price to travel to "..cityName[cid].." its 500 gold coins,a re you sure?", cid) <<<<< here
talkState[talkUser] = 1
moneyPrice[cid] = 500 <<<<<<< here
end
 
Last edited:
Solution
There's no problem with this method, however it could be cleaner.
For example, you could use 1 table and assign 3 variables inside of it for the info you need.
Lua:
local userInfo = {}

function greet(cid)
    userInfo[cid] = nil
end

-- inside creatureSayCallback
-- msgcontains
userInfo[cid] = {cityName = "Somewhere", destination = {x=x, y=y, z=z}, cost = 500}
Although I'm not sure why you'd ever need to do this, you could just set up a config with each city + position + cost in a table at the top of the script, then attempt to access that table with whatever the player says, for example:
Lua:
local cities = {
    -- city name must be lowercase
    ["somewhere"] = {destination = {x=x, y=y, z=z}, cost = 500}
}

-- inside creatureSayCallback...
There's no problem with this method, however it could be cleaner.
For example, you could use 1 table and assign 3 variables inside of it for the info you need.
Lua:
local userInfo = {}

function greet(cid)
    userInfo[cid] = nil
end

-- inside creatureSayCallback
-- msgcontains
userInfo[cid] = {cityName = "Somewhere", destination = {x=x, y=y, z=z}, cost = 500}
Although I'm not sure why you'd ever need to do this, you could just set up a config with each city + position + cost in a table at the top of the script, then attempt to access that table with whatever the player says, for example:
Lua:
local cities = {
    -- city name must be lowercase
    ["somewhere"] = {destination = {x=x, y=y, z=z}, cost = 500}
}

-- inside creatureSayCallback

local city = cities[msg:lower()]
if city then
    -- the city they want exists, ask if they want to travel
    -- do other stuff that you need
    -- you can access the variables inside city like this: city.destination, city.cost
end
 
Solution
Thanks bro, your first way its much better, and i dont use the second one because im thinking about a different script, without fixed values and strings
 
Back
Top