Something like this:
Code:
function FarmerGriffith_Events(Unit, Event)
if Event == 18 then -- if spawned
Unit:RegisterEvent("SendChatMessage_NearbyPlayers", 1000, 0)
else -- if died
Unit:RemoveEvents()
end
end
function ResetEvent(Unit)
Unit:RegisterEvent("SendChatMessage_NearbyPlayers", 1000, 0)
end
function SendChatMessage_NearbyPlayers(Unit)
local found = false
for _,v in pairs(Unit:GetInRangePlayers()) do -- for each nearby player, as the variable: v
if Unit:GetDistanceYards(v) < 40 and v:IsAlive() then -- if within 40 yards of unit and alive then
found = true -- say we found a nearby player
break -- we don't need to find another player so stop looping through all nearby players
end
end
if found then -- we found a player
Unit:SendChatMessage(12, 0, "Hey, Can you please help me?")
Unit:RemoveEvents() -- stop this being sent every second
Unit:RegisterEvent("ResetEvent", 60000, 1) -- start checking every second again in 1 minute
end
end
RegisterUnitEvent(20123, 4, "FarmerGriffith_Events") -- on dead
RegisterUnitEvent(20123, 18, "FarmerGriffith_Events") -- on spawn