Jump to content

Radio Speaker


Function

Recommended Posts

[CLIENT]

--------------------------------------------------------------------------------
url = "http://91.121.157.114:8421/stream"
--------------------------------------------------------------------------------

function playsound()
  local x,y,z = getElementPosition(localPlayer)
  sound = playSound3D(url,x,y,z)
  setSoundMaxDistance(sound,50)
end
addEvent("playsound",true)
addEventHandler("playsound",root,playsound)

function stopsound()
  stopSound(sound)
end
addEvent("stopsound",true)
addEventHandler("stopsound",root,stopsound)
[SERVER]

function createSpeaker(player)
  if isPedInVehicle(player) then
    outputChatBox("#ff0000[SPEAKER] #ffffffYou can't create a speaker while you are in a vehicle.",player,255,255,255,true) return end
    if isElementInWater(player) then
      outputChatBox("#ff0000[SPEAKER] #ffffffYou can't create a speaker while you are in the water.",player,255,255,255,true) return end
      local x,y,z = getElementPosition(player)
      if not isElement(speaker) then
        speaker = createObject(2229,x,y,z-1)
        triggerClientEvent("playsound",root,x,y,z)
        outputChatBox("#ff0000[SPEAKER] #ffffffYou have succesfully created a speaker.",player,255,255,255,true)
      else
        local sx,sy,sz = getElementPosition(speaker)
        outputChatBox("#ff0000[SPEAKER] #ffffffYou have already created a speaker at ("..string.format("%.2f",sx).." "..string.format("%.2f",sy).." "..string.format("%.2f",sz)..")",player,255,255,255,true)
      end
    end
    addCommandHandler("cspeaker",createSpeaker)

function destroySpeaker(player)
  if isElement(speaker) then
    destroyElement(speaker)
    outputChatBox("#ff0000[SPEAKER] #ffffffYou have succesfully destroyed the speaker.",player,255,255,255,true)
    triggerClientEvent("stopsound",root)
  else
    outputChatBox("#ff0000[SPEAKER] #ffffffYou haven't created a speaker.",player,255,255,255,true)
  end
end
addCommandHandler("dspeaker",destroySpeaker)

function destroySpeakerOnQuit()
  if isElement(speaker) then
    destroyElement(speaker)
    triggerClientEvent("stopsound",root)
  end
end
addEventHandler("onPlayerQuit",root,destroySpeakerOnQuit)

Please guys can someone try this script with a friend and see if it works or is there something wrong?

Edited by Function
  • Like 1
Link to comment
--client
function playsound()
  local x,y,z = getElementPosition(localPlayer)
  ...
end

--server
triggerClientEvent("playsound",root,x,y,z)

You're sending the XYZ coordinates but then ignoring them completely on the client and instead using the local player's position to spawn sound? This means sound will spawn in a different place for every player, more specifically - every player will get sound played at the position they're in, not where the speaker is.

Change this to the following

--client
function playsound(x,y,z)
  ...
end

--server
triggerClientEvent("playsound",root,x,y,z)

 

Link to comment

Thank you very much bro!

And whats about

function stopsound()
  stopSound(sound)
end
addEvent("stopsound",true)
addEventHandler("stopsound",root,stopsound)

is there something wrong? Why do I not need the x, y, z coordinates on stop sound?

Edited by Function
Link to comment

You do not need the position. Read the wiki page about the function and know what the arguments are.

B| Also a pro tip:

It's a bad idea to name your functions the same name as MTA's functions because you will create an infinite loop where the function calls itself.

Use a bit of diversity(but not too much) when creating the names.

Link to comment
19 minutes ago, Mr.Loki said:

B| Also a pro tip:

It's a bad idea to name your functions the same name as MTA's functions because you will create an infinite loop where the function calls itself.

Use a bit of diversity(but not too much) when creating the names.

Not true in this case as Lua is case sensitive. But you're right, in this case it actually doesn't make much sense to create a new function to call just one other function, just use 'anonymous' function by putting it straight into addEventHandler.

addEventHandler("event", element, function () otherFunction(arg) end)

 

  • Like 1
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...