Attempt to index global 'self' a nil value.

This famous error that it returns nil is really annoying.
I don’t really see what’s wrong in my code :


timescale = 1.00;
local heartbeat1 = CreateSound( self.Entity, "heartbeat.wav" )
 
function slowmoon()
    RunConsoleCommand("play","slowmoon.wav")
    timer.Create("timerslow",0.0075,99,slow)
    timer.Simple( 1, heartbeat )
end


function slowmooff()
    RunConsoleCommand("play","slowmooff.wav")
    timer.Create("timerfast",0.0075,99,fast)
    stopheartbeat()
end


function slow()
    timescale = math.Approach(timescale, 0.01, 0.01)
    MsgN(timescale)
    game.ConsoleCommand("host_timescale " .. timescale .. "
")
    game.ConsoleCommand("pp_motionblur_addalpha " .. timescale * 1.5 .. "
")
end


function fast()
    timescale = math.Approach(timescale, 1.00, 0.01)
    MsgN(timescale)
    game.ConsoleCommand("host_timescale " .. timescale .. "
")
    game.ConsoleCommand("pp_motionblur_addalpha " .. timescale * 1.5 .. "
")
end


function heartbeat()
    heartbeat1:Play()
end


function stopheartbeat()
    heartbeat1:FadeOut( 0.5 )
end


if SERVER then
concommand.Add("SlowMoON",slowmoon,"Used to slow time down.")
concommand.Add("SlowMoOFF",slowmooff,"Used to speed up time.")
end

Any ideas why ?

top line. self.Entity is the problem. What is it refering to? Is it defined?

I’m just trying to declare a sound actually.

Well now that you’ve edited it, it’s the second line now ofcourse …

I smell copypasting without understanding what things mean.

self.Entity. When you say self, it uses the object of the function. If it’s in the script scope, self is nil. Feed the entity to CreateSound instead of self.Entity. Since I assume you’re doing it on the player and that this is clientside, you’ll want to use LocalPlayer() as your entity.