SENT help

So, I’m trying to make a jukebox where if you press your use on it it plays a song, and when you press E again it stops it. It plays the song but I have two problems. From pretty much all the map, I would like to make it so when your within 25 feet. And my second problem is I don’t know how I would make it so if you press your use key on it again it would stop.

[LUA]
AddCSLuaFile(“shared.lua”)
include(“shared.lua”)

AddCSLuaFile( “cl_init.lua” )
AddCSLuaFile( “shared.lua” )

include(‘shared.lua’)

function ENT:SpawnFunction( ply, tr )
if ( !tr.Hit ) then return end
local ent = ents.Create(“jukebox”)
ent:SetPos( tr.HitPos + tr.HitNormal * 16 )
ent:Spawn()
ent:Activate()

return ent

end

function ENT:Initialize()

self.Entity:SetModel( "models/props_borealis/bluebarrel001.mdl" )
self.Entity:PhysicsInit( SOLID_VPHYSICS )
self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
self.Entity:SetSolid( SOLID_VPHYSICS )

    local phys = self.Entity:GetPhysicsObject()
if (phys:IsValid()) then
	phys:Wake()
end

end

function ENT:Use()

self.Entity:EmitSound("weapons/kickinthehead.wav", 100, 100 )

– self.Entity:StopSound(“weapons/kickinthehead.wav”)
end

function ENT:Think()
end
[/LUA]

Sorry for asking so many questions, I’m a slow learner. Can someone also give me a link to Overv search engine.

[lua]

function ENT:Initialize()

self.Entity:SetModel( "models/props_borealis/bluebarrel001.mdl" )
self.Entity:PhysicsInit( SOLID_VPHYSICS )
self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
self.Entity:SetSolid( SOLID_VPHYSICS )

    local phys = self.Entity:GetPhysicsObject()
if (phys:IsValid()) then
	phys:Wake()
end
    
    self.IsPlaying = false

end

function ENT:Use()

if self.IsPlaying then
    self.Entity:StopSound("weapons/kickinthehead.wav")
        self.IsPlaying = false
        return
    else
   	    self.Entity:EmitSound("weapons/kickinthehead.wav", 100, 100 )
        self.IsPlaying = true
        return
    end

end
[/lua]

That should sort the stopping the sound, do you understand how I did it?

Uh, no I don’t Can you explain please.

[editline]09:59PM[/editline]

When I press E on it it plays the song and then when i try to turn it off by pressing E on it it just starts the song over again

http://luasearch.overvprojects.nl/index.php



self.Entity:SetUseType( SIMPLE_USE )


Put that in the initialise function.

He set a variable isPlaying to false and when he pressed the entity, it checked if isPlaying was set to false, if it was, set it to true, and then started playing the song.

If isPlaying was set to true when you activated the entity, it would set isPlaying to false, and stop the sound.

[lua]function ENT:Use()
self.IsPlaying = self.IsPlaying and (self.Entity:StopSound(“weapons/kickinthehead.wav”) and not self.IsPlaying) or (self.Entity:EmitSound(“weapons/kickinthehead.wav” , 100 , 100) and not self.IsPlaying)
return;
end[/lua]

This logic might be easier to understand. It’s how I’d personally do it, and makes more sense to me. I doubt it will to a beginner, but lash at it. It does the same as the code posted above, except with less un-necessary stuff. As I said, it probably looks complicated to most people, but once you grasp the idea of it, it’s easy as hell.

Ah Thanks, I think I understand it now.

Even with that it still keeps starting the song from the beginning. I also tried FlapJack’s and I got the same thing.