Code:
function OnSpawn(pUnit, Event) -- This defines the functions name, and what the function uses.
pUnit:RegisterEvent("CheckForPlayers", 1000, 0) -- This says the the pUnit (The NPC) is going to use the "CheckForPlayers" command every 1000 milliseconds, until the server dies or the Event is removed.
end -- This tells the engine that that is everything that is in the function "OnSpawn"
function CheckForPlayers(pUnit) -- Defines another function, "CheckForPlayers"
local plr = pUnit:GetClosestPlayer() -- This is making a local word, "plr" whenever the word "plr" comes up, it now means what it is equal to.
if plr ~= nil then --If nobody is nearby, then...
if pUnit:GetDistanceYards(plr) < 10 then --check if there is a player within 10 yards, if there is..
pUnit:SendChatMessage(12,0,"Its dangerous out there!") --Send this chat message in say, then...
pUnit:RemoveEvents() -- Remove all events, including the one that pulses every 1000 milliseconds to check if someone is nearby.
end -- closes the check for if anyone is nearby
end -- closes the check for if player is equal to anything
end -- closes the CheckForPlayers function
RegisterUnitEvent(99990, 18, "OnSpawn") -- This script binds to NPCid 99990 when it spawns and starts at the function "OnSpawn"
I'm not going to hand the answer directly to you on a silver platter, use the notes I have given at the end of each line and remove the line that is causing it.