Jump to content

Question about Add Command handler


ginger_buddy

Recommended Posts

is it possible to use command handler to change a variable in a particular script?

For example (in sound.script)

SoundEnabled = true

function sound()

if SoundEnabled then

[some code] 

end

AddEventHandler(......)

 

now this function is my disable and enable sound function:

 

  
function disablePlayLocalMusic(playerSource) 
    if (playerSource) then 
        if(SoundEnabled) then 
            SoundEnabled = false 
            outputChatBox ( "Ambience Sounds Disabled!", playerSource ) 
        end 
        elseif not (GroveEnable) then 
            outputChatBox ( "Ambience Sounds Already Disabled!", playerSource ) 
        end 
    end  
end 
addCommandHandler ( "OffAmbience", disablePlayLocalMusic) 
  
function enablePlayLocalMusic(playerSource) 
    if (playerSource) then 
        if(SoundEnabled) then 
            outputChatBox ( "Ambience Sounds Already Enabled!", playerSource ) 
        end 
        elseif not (SoundEnabled) then 
            SoundEnabled= true 
            outputChatBox ( "Ambience Sounds Enabled!", playerSource ) 
        end 
    end  
end 
addCommandHandler ( "OnAmbience", enablePlayLocalMusic) 
  

I want to use them to change soundEnabled to either true or false

However in the game when I typed /OnAmbience or /OffAmbience, nothing happens and the function sound() also stops working. What did I do wrong? or does this method even work?

Link to comment

try this:

local SoundEnabled = false 
  
function disablePlayLocalMusic(playerSource, command) 
    if (playerSource) then 
        if(SoundEnabled) then 
            SoundEnabled = false 
            outputChatBox ( "Ambience Sounds Disabled!", playerSource ) 
        elseif not (SoundEnabled) then 
            outputChatBox ( "Ambience Sounds Already Disabled!", playerSource )          
        end 
    end  
end 
addCommandHandler ( "OffAmbience", disablePlayLocalMusic) 
  
function enablePlayLocalMusic(playerSource, command) 
    if (playerSource) then 
        if(SoundEnabled) then 
            outputChatBox ( "Ambience Sounds Already Enabled!", playerSource ) 
        elseif not (SoundEnabled) then 
            SoundEnabled= true 
            outputChatBox ( "Ambience Sounds Enabled!", playerSource ) 
        end          
    end  
end 
addCommandHandler ( "OnAmbience", enablePlayLocalMusic) 

PD: Where is function sound() exactly?

Link to comment

Thank you Very much. The Sound() function (written above the disable and enable function in the same script file) just plays random music from music table at player location every once awhile.

It appears that my original disable and enable function has incorrect syntax, now the function name links to the 'end'.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...