Null Entity Error

I have been trying to spawn in an entity on a map (I’m realtively new to entities and how they work) but I keep getting this error:

[ERROR] lua/autorun/server/piano.lua:3: Tried to use a NULL entity!
  1. SetPos - [C]:-1
   2. pianostuff - lua/autorun/server/piano.lua:3
    3. unknown - lua/autorun/server/piano.lua:7

The code I am using is this:
[lua]local piano = ents.Create( “gmt_instrument_piano” )
function pianostuff()
piano:SetPos(Vector(6.802947, -328.385437, 0.370615))
timer.Simple( 1, function() pianostuff() end )[/lua]
What have I done wrong and how can I fix it?
Thanks for any help in adavance :slight_smile:

The entity you create isn’t valid.

Not valid as in it doesn’t exist? I’m very sure it exists as the entity is in the folder.

Its not valid, something prevents it from being created properly

Hmm. Not sure what could be doing this. Is there anything wrong with the code I’m using? I’m trying to spawn in just the normal playable piano.

If you put call an entity into a timer, always make sure you do an IsValid check before you call it again.

Why are you creating the entity the moment the lua file loads??

Create the entity when you need it and remove it when you don’t need it.

I want it to be on the map after mapchanges and such permanently. How would you suggest doing it?

Use an InitPostEntity hook. The InitPostEntity event occurs when the entities of a map have been loaded.

Sorted! Cheers :slight_smile:
Code for anyone that’s interested:
[lua]local function PianoSpawn()
local piano = ents.Create( “gmt_instrument_piano” )
piano:SetPos(Vector(6.802947, -328.385437, 0.370615))
hook.Add( “InitPostEntity”, “Piano”, PianoSpawn )[/lua]