• 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 Making NPCs

empasor123

New Member
Joined
Jul 17, 2024
Messages
15
Reaction score
1
GitHub
empasor123
Hello there OTland.

I am trying to make NPCs using tfs 1.4.2

Firstly i make for example:
1. data/npc > Creating test_npc.xml
2. data/npc/scripts > creating test_npc.lua

Making it with really easy code such
" function onCreatureSay(player, type, msg)
if msg:lower() == "hi" then
selfSay("Hello, I am a simple test NPC.", player)
end
end"

But i cant make the NPCs to spawn?
I put into RME and trying to make them spawn automatic when server restart
and i have tried to spawn it manuelly on GM.

Any ideas? everything is welcome <3 <3
 
Probably getting an error in the npc when the server starts.

The npc requires a few other things in the .lua file.

Here's an example of 'the most basic npc'.

XML:
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Test NPC" script="test_npc.lua" walkinterval="2000" floorchange="0">
    <health now="100" max="100" />
    <look type="139" head="20" body="39" legs="45" feet="7" addons="0" />
</npc>
LUA:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

function onCreatureAppear(cid)          npcHandler:onCreatureAppear(cid)          end
function onCreatureDisappear(cid)       npcHandler:onCreatureDisappear(cid)       end
function onCreatureSay(cid, type, msg)  npcHandler:onCreatureSay(cid, type, msg)  end
function onThink()                      npcHandler:onThink()                      end

local function greetCallback(cid)
    local player = Player(cid)
    npcHandler:setMessage(MESSAGE_GREET, "Hello, I am a simple {test} NPC.")
    return true
end

local function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
 
    local player = Player(cid)

    if msgcontains(msg, "test") then
        npcHandler:say("This is a test message.", cid)
    end

    return true
end

local function onAddFocus(cid)
    -- empty
end

local function onReleaseFocus(cid)
    -- empty
end

npcHandler:setCallback(CALLBACK_ONADDFOCUS, onAddFocus)
npcHandler:setCallback(CALLBACK_ONRELEASEFOCUS, onReleaseFocus)

npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
also to note here if you gonna use linux make sure ALL FILES have correct names in the data e.g npc.lua not Npc.lua etc because linux will tell you npcs dont exist and wont put them on map during start
 
Back
Top