Indexing a value from a different function?

Hello, this is apm for probably the billionth time, alot of my code is running smoothly at the moment, but I have a small question…
Is it possible to index a value from a different function or to “carry” a value from one seperate function to another? for instance:


function ENT:Initialize()
blablabla...
local ragdoll = ents.Create("prop_ragdoll")
ragdoll:Spawn()
end

and then carry “ragdoll” to this function:



function ENT:Think()
blablabla...
ragdoll:Remove()
end

I’m attempting something similar to this with CSoundPatch and i need the sound to begin in one function and stop when the npc dies or is deleted, but that aside, is it possible to do so…
If it’s so obvious that it’s tearing my face off while my eyes are open… then criticize, it helps.

Either don’t make it local or return it.

This is the code that i’m trying to index.


CSP = CreateSound( player.GetByID( 1 ), self.Theme )
CSP:SetSoundLevel(100)

if CSP:IsPlaying() == false then
CSP:Play()

So all there is left to do is to return it somehow…

[lua]function Something()

// Do your stuff

return CSP

end

function Lol()

CSP = Something()

end[/lua]




[ERROR] addons/war of the worlds - tripod snpc/lua/entities/npc_tripod/init.lua:117: 'end' expected (to close 'function' at line 89) near 'if'
  1. unknown - addons/war of the worlds - tripod snpc/lua/entities/npc_tripod/init.lua:0



Which actually IS closed so, whatthefuck…
Anyway, thanks for the help CrashLemon!
I can put your name on to the credit list upon upload (whatever century that may be)

You forgot to end the if statement, after CSP:IsPlaying().

Previously, I had added two ends but left them out to shorten it, the statement was ended but for some reason, it’s requesting i end it again, continuously…

assuming they are in the same ent:

[lua]local ragdoll = false
function ENT:Initialize()
blablabla…
ragdoll = ents.Create(“prop_ragdoll”)
ragdoll:Spawn()
end

function ENT:Think()
blablabla…
if ragdoll then
ragdoll:Remove()
end
end[/lua]

or

[lua]function ENT:Initialize()
blablabla…
self.ragdoll = ents.Create(“prop_ragdoll”)
ragdoll:Spawn()
end

function ENT:Think()
blablabla…
if IsValid(self.ragdoll) then
self.ragdoll:Remove()
end
end[/lua]