Jump to content

stopSound individually


Recommended Posts

how to make the sound stop individually?

Two players start the sound at the same time only one sound will stop when we use the stop command and the sound that I keep is impossible to stop.

:cry:

local url = "http://www.181.fm/winamp.pls?station=181-power&style=mp3&description=Power%20181%20(Top%2040)&file=181-power.pls" 
  
sound = {} 
function playTheSound(x, y, z, vehicle) 
    sound[vehicle] = playSound3D(url, x, y, z) 
    if (isElement(vehicle)) then attachElements(sound[vehicle], vehicle) end 
     
     
end 
addEvent("playTheSound", true) 
addEventHandler("playTheSound", getRootElement(), playTheSound) 
  
function stopTheSound(vehicle) 
    stopSound(sound[vehicle]) 
end 
addEvent("stopTheSound", true) 
addEventHandler("stopTheSound", getRootElement(), stopTheSound) 

Link to comment

Why arent you checking if the sound already exists before creating a new sound?

If you do not check if the sound element already exists, then it will just ignore the old sound so sound[vehicle] will only give the newly created sound.

Maybe try this:

  
local url = "http://www.181.fm/winamp.pls?station=181-power&style=mp3&description=Power%20181%20(Top%2040)&file=181-power.pls" 
  
sound = {} 
function playTheSound(x, y, z, vehicle) 
    if not isElement(vehicle) then outputChatBox("Vehicle does not exist") return false end -- Checks if vehicle exist, and returns if vehicle does not exist 
    if isElement(sound[vehicle]) then outputChatBox("Sound is already playing") return false end -- Checks if sound exist, and returns if sound does exist 
  
    local theSound = playSound3D(url, x, y, z) 
    if theSound then 
        sound[vehicle] = theSound 
        attachElements(sound[vehicle], vehicle) 
    else 
        outputChatBox("Sound failed to play") 
        return false 
    end 
    
    
end 
addEvent("playTheSound", true) 
addEventHandler("playTheSound", getRootElement(), playTheSound) 
  
function stopTheSound(vehicle) 
    stopSound(sound[vehicle]) 
end 
addEvent("stopTheSound", true) 
addEventHandler("stopTheSound", getRootElement(), stopTheSound) 
  

Link to comment

I started using the script that AfuSensi sent me.

but when I use the command to stop the sound, only the marker is destroyed.

:cry:

xXMADEXx and AfuSensi can you help me ?

side client

local url = "http://www.181.fm/winamp.pls?station=181-power&style=mp3&description=Power%20181%20(Top%2040)&file=181-power.pls" 
  
sound = {} 
function playTheSound(x, y, z, vehicle) 
    if not isElement(vehicle) then outputChatBox("Vehicle does not exist") return false end -- Checks if vehicle exist, and returns if vehicle does not exist 
    if isElement(sound[vehicle]) then outputChatBox("Sound is already playing") return false end -- Checks if sound exist, and returns if sound does exist 
  
    local theSound = playSound3D(url, x, y, z) 
    if theSound then 
        sound[vehicle] = theSound 
        attachElements(sound[vehicle], vehicle) 
    else 
        outputChatBox("Sound failed to play") 
        return false 
    end 
    
    
end 
addEvent("playTheSound", true) 
addEventHandler("playTheSound", getRootElement(), playTheSound) 
  
function stopTheSound(vehicle) 
    stopSound(sound[vehicle]) 
end 
addEvent("stopTheSound", true) 
addEventHandler("stopTheSound", getRootElement(), stopTheSound) 

side server

speakerObject = {} 
  
function createSpeaker(thePlayer) 
    local x, y, z = getElementPosition(thePlayer) 
    speakerObject[thePlayer]  = createMarker(x, y, z, "corona", 0.1, 255, 0, 0) 
    outputChatBox("You have Succesfully created a speaker!", thePlayer, 0, 250, 0) 
    if (isPedInVehicle(thePlayer)) then 
        local vehicle = getPedOccupiedVehicle(thePlayer) 
        attachElements(speakerObject[thePlayer], vehicle) 
        triggerClientEvent(root,"playTheSound", root, x, y, z, vehicle) 
    else 
        triggerClientEvent(root,"playTheSound", root, x, y, z) 
    end 
end 
addCommandHandler("startsound", createSpeaker) 
  
function deleteSpeaker(thePlayer) 
    if (isElement(speakerObject[thePlayer])) then 
        destroyElement(speakerObject[thePlayer]) 
        outputChatBox("You have Succesfully destroyed the Speaker!", thePlayer, 0, 0, 255) 
                triggerClientEvent(root,"stopTheSound", root) 
    else 
        outputChatBox("Speaker is not created!", thePlayer, 250, 0, 0) 
    end 
end 
addCommandHandler("stopsound", deleteSpeaker) 

Link to comment

-- Server side

local speakerObject = {} 
  
function createSpeaker(thePlayer) 
    local x, y, z = getElementPosition(thePlayer) 
    speakerObject[thePlayer]  = createMarker(x, y, z, "corona", 0.1, 255, 0, 0) 
    outputChatBox("You have Succesfully created a speaker!", thePlayer, 0, 250, 0) 
    if (isPedInVehicle(thePlayer)) then 
        local vehicle = getPedOccupiedVehicle(thePlayer) 
        attachElements(speakerObject[thePlayer], vehicle) 
        triggerClientEvent(root,"playTheSound", root, x, y, z, vehicle) 
    else 
        triggerClientEvent(root,"playTheSound", root, x, y, z) 
    end 
end 
addCommandHandler("startsound", createSpeaker) 
  
function deleteSpeaker(thePlayer) 
    if (isElement(speakerObject[thePlayer])) then 
        destroyElement(speakerObject[thePlayer]) 
        outputChatBox("You have Succesfully destroyed the Speaker!", thePlayer, 0, 0, 255) 
        if (isPedInVehicle(thePlayer)) then 
            local vehicle = getPedOccupiedVehicle(thePlayer) 
            triggerClientEvent(root,"stopTheSound", root,vehicle) 
        end  
    else 
        outputChatBox("Speaker is not created!", thePlayer, 250, 0, 0) 
    end 
end 
addCommandHandler("stopsound", deleteSpeaker) 

-- Client side

llocal url = "http://www.181.fm/winamp.pls?station=181-power&style=mp3&description=Power%20181%20(Top%2040)&file=181-power.pls" 
local sound = {} 
  
function playTheSound(x, y, z, vehicle) 
    if not isElement(vehicle) then 
        outputChatBox("Vehicle does not exist") 
        return false  
    end  
    if isElement(sound[vehicle]) then 
        stopSound(sound[vehicle]) 
    end  
        local theSound = playSound3D(url, x, y, z) 
            if theSound then 
            sound[vehicle] = theSound 
            attachElements(sound[vehicle], vehicle) 
        else 
            outputChatBox("Sound failed to play") 
        return false 
    end 
end 
addEvent("playTheSound", true) 
addEventHandler("playTheSound", getRootElement(), playTheSound) 
  
function stopTheSound(vehicle) 
    if isElement(sound[vehicle]) then 
        stopSound(sound[vehicle]) 
    end  
end 
addEvent("stopTheSound", true) 
addEventHandler("stopTheSound", getRootElement(), stopTheSound) 

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