Jump to content

Add sound


Recommended Posts

Posted

Hi everyone,

I have the resource water_level (https://community.multitheftauto.com/index.php?p= ... ils&id=630)

Now i want if the water level is going up, that he played a sound "alarm.mp3"

I have readed that i have to add in the meta.xml this:

"alarm.mp3" type="client" /> 

But where and what have i to do here:

water_level.lua

level = 0 
highlevel = 0 
floodWater1 = 0 
  
function setAllWatersLevel(level) 
    setWaterLevel(level) 
    if isElement(floodWater1) then 
        if (getElementType(floodWater1) == "water") then 
            if (level > 0.2) then 
                setWaterLevel(floodWater1, level) 
            else 
                setWaterLevel(floodWater1, -500) 
            end 
        end 
    end 
end 
  
function checkLevel(glevel) 
    local allow_negative = get("allow_negative") 
    if ((allow_negative == "false") and (tonumber(glevel) < 0)) then 
        return false 
    end 
    return true 
end 
  
function setFloodWeather() 
    local flood_weather = get("flood_weather") 
    if (flood_weather ~= "") then 
        setWeather(tonumber(flood_weather)) 
    end 
end 
  
function setFloodEndWeather() 
    local flood_end_weather = get("flood_end_weather") 
    if (flood_end_weather ~= "") then 
        setWeather(tonumber(flood_end_weather)) 
    end 
end 
  
function waterLevel ( source,  glevel ) 
    if ( checkLevel(glevel) == true ) then 
        highlevel = tonumber ( glevel ) 
        if (highlevel == nil) then 
            highlevel = 0 
        end 
        if (highlevel ~= level) then 
            if ( isTimer(waterTimer)) then 
                killTimer(waterTimer) 
            end 
            --water is going up 
            if (highlevel > level) then 
                setFloodWeather() 
                waterTimer = setTimer ( addSomeWater, 100, 0, highlevel ) 
                if (get("show_messages") == "true") then 
                    if (highlevel > -0.2) then 
                        outputChatBox ( "ALERT: A FLOOD IS EMERGING! WE ESTIMATE A WATER LEVEL OF "..tostring(highlevel).." meters", getRootElement(), 255, 0, 0 ) 
                    else 
                        outputChatBox ( "INFO: A DROUGHT IS LEAVING! WE RETURN TO A WATER LEVEL OF "..tostring(highlevel).." meters", getRootElement(), 0, 255, 0 ) 
                    end 
                end 
            else --water is going down 
                setFloodEndWeather() 
                waterTimer = setTimer ( removeSomeWater, 100, 0, highlevel ) 
                if (get("show_messages") == "true") then 
                    if (highlevel > -0.2) then 
                        outputChatBox ( "INFO: A FLOOD IS LEAVING! WE RETURN TO A WATER LEVEL OF "..tostring(highlevel).." meters", getRootElement(), 0, 255, 0 ) 
                    else 
                        outputChatBox ( "ALERT: A DROUGHT IS COMING! WE ESTIMATE A WATER LEVEL OF "..tostring(highlevel).." meters", getRootElement(), 255, 0, 0 ) 
                    end 
                end 
            end 
            outputDebugString ( "Water Level changed by " .. getPlayerName ( source ) .. " to a level " .. highlevel .. ".", 3 ) 
        end 
    end 
end 
addEvent( "onWaterLevel", true ) 
addEventHandler( "onWaterLevel", getRootElement(), waterLevel ) 
  
function showClientGui(source, command, highlevel) 
    local neg = get("allow_negative") 
    if (get("restrict_to") ~= "") then 
        if ( isObjectInACLGroup ( "user." .. getAccountName ( getPlayerAccount ( source )), aclGetGroup ( get("restrict_to") ) ) ) then 
            triggerClientEvent( source, "onShowWindow", getRootElement(), level, neg, highlevel) 
        end 
    else 
        triggerClientEvent( source, "onShowWindow", getRootElement(), level, neg, highlevel) 
    end 
end 
addCommandHandler("water", showClientGui) 
  
function addSomeWater ( highlevel ) 
    local thelevel = level 
    local speed = tonumber(get("speed")) 
    level = thelevel + tonumber(speed) 
  
    setAllWatersLevel(level) 
     
    if (level >= highlevel) then 
        level = highlevel 
        if ( isTimer(waterTimer)) then 
            killTimer(waterTimer) 
        end 
        setFloodEndWeather() 
    end 
end 
  
function removeSomeWater ( highlevel ) 
    local thelevel = level 
    local speed = tonumber(get("speed")) 
    level = thelevel - speed 
     
    setAllWatersLevel(level) 
     
    if (level <= highlevel) then 
        level = highlevel 
        if ( isTimer(waterTimer)) then 
            killTimer(waterTimer) 
        end 
    end 
end 
  
function initialize() 
    setWaterLevel(0) 
    floodWater1 = createWater ( -2998, -2998, -500, 2998, -2998, -500, -2998, 2998, -500, 2998, 2998, -500) 
end 
addEventHandler ( "onResourceStart", getResourceRootElement(getThisResource()), initialize) 
  
function destroy() 
    if ( isTimer(waterTimer)) then 
        killTimer(waterTimer) 
    end 
    setWaterLevel(floodWater1, -500) 
    setWaterLevel(0) 
    setFloodEndWeather() 
end 
addEventHandler ( "onResourceStop", getRootElement(), destroy) 
  
function onPlayerJoin() 
    setAllWatersLevel(level) 
end 
addEventHandler( "onPlayerJoin", getRootElement(), onPlayerJoin ) 

You can see this:

function waterLevel ( source,  glevel ) 
    if ( checkLevel(glevel) == true ) then 
        highlevel = tonumber ( glevel ) 
        if (highlevel == nil) then 
            highlevel = 0 
        end 
        if (highlevel ~= level) then 
            if ( isTimer(waterTimer)) then 
                killTimer(waterTimer) 
            end 
            --water is going up 
            if (highlevel > level) then 
                setFloodWeather() 
                waterTimer = setTimer ( addSomeWater, 100, 0, highlevel ) 
                if (get("show_messages") == "true") then 
                    if (highlevel > -0.2) then 
                        outputChatBox ( "ALERT: A FLOOD IS EMERGING! WE ESTIMATE A WATER LEVEL OF "..tostring(highlevel).." meters", getRootElement(), 255, 0, 0 ) 
                    else 
                        outputChatBox ( "INFO: A DROUGHT IS LEAVING! WE RETURN TO A WATER LEVEL OF "..tostring(highlevel).." meters", getRootElement(), 0, 255, 0 ) 
                    end 
                end 
            else --water is going down 

I hope someone can help me.

I am learning to script MTA :)

Posted
playSound 

╔═╦╦╦╗╔╦═╦═╦═╦═╦╦╦╦╦╦╦═╦╦╦╦╗

║║║║║╠╝║║╚╣║║║║║║║║║║║║║║║║║

║║║║║╚╦╦═╦╦╦╦╣║║║║║║║║║║║║║║

║║║║╠═╣╠╝╔══╝║║║║║╟╢║║║║║╟╢║

╚╩══╩══╩═╩═══╩╩══╩╩═╩╩╩═╩╩═╝

«سبحانك اللهم وبحمدك أشهد أن لا إله إلا أنت أستغفرك وأتوب إليك»

M7mdAl7arthy : لتواصل سكايب

Posted

I had try that, but what have i to do.

In the meta it is good, but in the script?

I had try that, but there wasn't a sound, i think i do something wrong, thats the reason for this topic...

  • Moderators
Posted

https://wiki.multitheftauto.com/wiki/PlaySound

local sound = playSound("alarm.mp3")

Make sure your mta sounds are ON. (settings)

It does work.

  
                    if (highlevel > -0.2) then 
                        outputChatBox ( "ALERT: A FLOOD IS EMERGING! WE ESTIMATE A WATER LEVEL OF "..tostring(highlevel).." meters", getRootElement(), 255, 0, 0 ) 
                    local sound = playSound("alarm.mp3") 
                    else 
  

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

Posted

I get the same error,

[2012-05-12 18:54:20] ERROR: unzipped\water_level2\water_level.lua:57: attempt to call global 'playSound' (a nil value)

Posted
I get the same error,

[2012-05-12 18:54:20] ERROR: unzipped\water_level2\water_level.lua:57: attempt to call global 'playSound' (a nil value)

you problem here !!

where level global ??

  
function showClientGui(source, command, highlevel) 
    local neg = get("allow_negative") 
    if (get("restrict_to") ~= "") then 
        if ( isObjectInACLGroup ( "user." .. getAccountName ( getPlayerAccount ( source )), aclGetGroup ( get("restrict_to") ) ) ) then 
            triggerClientEvent( source, "onShowWindow", getRootElement(), level, neg, highlevel) 
        end 
    else 
        triggerClientEvent( source, "onShowWindow", getRootElement(), level, neg, highlevel) 
    end 
end 
addCommandHandler("water", showClientGui) 
  

تذكر عند تصفحك للأنترنت قوله تعالى: (( وهو معكم أين ما كنتم والله بما تعملون بصير )) ا

  • Moderators
Posted

maybe you must put it in an unzipped folder?

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

  • Moderators
Posted

You know this is cliend side?

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

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...