first off, you should know that something despawning when it goes off screen long enough is standard smbx behavior. the reason for the oddities is that there's no code in the lua file that handles the npc despawning gracefully
i'm not 100% sure of all the behaviors you need for the npc, but adding the following code should fix the issues you're having
first, add this in sphero.lua before the isHidden check in the onTickNPC function:
Code: Select all
if(v.despawnTimer == 1) then
for _, p in ipairs(Player.get()) do
if(v.section == p.section) then
v.despawnTimer = 2
end
end
end
if(v.despawnTimer <= 0) then
data.initialized = false
return
end
this prevents the npc from despawning as long as there's a player in the same section as itself, and reinitializes its data in the event that it does despawn
then, also in sphero.lua, add another check to the onDrawNPC early return, making it look like this:
Code: Select all
if (v.isHidden) or data.hidden or v.despawnTimer <= 0 then return end