• 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 NPC outfit changer

Fortera Global

Intermediate OT User
Joined
Nov 20, 2015
Messages
1,180
Solutions
2
Reaction score
117
Please help?


for monsters have one function:

<defense name="outfit" interval="5000" chance="10" monster="tarantula" duration="4000"/>

but for npc how work? any idea? I need for npc change the own outfit (others colors) every 1 second


Any idea?
 
Change the npc onThink to
Code:
function onThink()
    local cid = getNpcCid()
    local npc = Creature(cid)
    if npc then
        local outfit = npc:getOutfit()
        outfit.lookHead = math.random(0, 132)
        outfit.lookBody = math.random(0, 132)
        outfit.lookLegs = math.random(0, 132)
        outfit.lookFeet = math.random(0, 132)
        npc:setOutfit(outfit)
    end
    npcHandler:onThink()
end
 
hummm
but how?


Code:
<?xml version="1.0"?>
<npc name="Willian" script="willian.lua" walkinterval="0" floorchange="0">
<health now="100" max="100"/>
<look type="132" head="82" body="82" legs="82" feet="82" addons="3"/>
<parameters>
<parameter key="message_greet" value="Hello." />
</parameters>
<script interval="10000" script="willian.lua"/>
</npc>
 
The onThink is triggered every 1s so if you want to do it faster you can use addEvent. Keep in mind this will only work to make it faster, it wont work for values greater than 1000
Code:
function changeOutfit(cid)
    local npc = Creature(cid)
    if npc then
        local outfit = npc:getOutfit()
        outfit.lookHead = math.random(0, 132)
        outfit.lookBody = math.random(0, 132)
        outfit.lookLegs = math.random(0, 132)
        outfit.lookFeet = math.random(0, 132)
        npc:setOutfit(outfit)
    end
end

local interval = 100
function onThink()
    local cid = getNpcCid()
    for i = 1, 1000/interval do
        addEvent(changeOutfit, (i-1)*interval, cid)
    end
    npcHandler:onThink()
end
 
for completeness, if you want values higher then 1 second, you'd have to implement a counter.

As an example, 5 second interval
Lua:
local interval = 5
local counter = 0

function onThink()
    counter = counter + 1
    if counter == interval then
        counter = 0
        changeOutfit(getNpcCid())
    end
    npcHandler:onThink()
end
 
Change the npc onThink to
Code:
function onThink()
    local cid = getNpcCid()
    local npc = Creature(cid)
    if npc then
        local outfit = npc:getOutfit()
        outfit.lookHead = math.random(0, 132)
        outfit.lookBody = math.random(0, 132)
        outfit.lookLegs = math.random(0, 132)
        outfit.lookFeet = math.random(0, 132)
        npc:setOutfit(outfit)
    end
    npcHandler:onThink()
end
C++:
[Error - NpcScript Interface]
data/npc/scripts/Addoner.lua:onThink
Description:
data/npc/scripts/Addoner.lua:13: attempt to call global 'Creature' (a nil value)
stack traceback:
    data/npc/scripts/Addoner.lua:13: in function <data/npc/scripts/Addoner.lua:11>
 
Back
Top