Jump to content

Tokio

Recommended Posts

This is the code:

Spoiler

local streamURL = "http://stream.musicfm.hu:8000/musicfm.mp3" 
local radius = 150 
local volume = 1.0 
local s = false 

addEvent ( "khifizene" , true) 
addEventHandler("khifizene", root, 
function() 
if s then stopSound(soundd) s = false detachElements(soundd, localPlayer) return end 
    s = true 
    local x, y, z = getElementPosition(localPlayer) 
    local soundd = playSound3D(streamURL, x, y, z, true) 
    setSoundVolume(soundd , volume) 
    setSoundMaxDistance(soundd , radius) 
	attachElements(soundd, localPlayer)
end) 

fileDelete("c.lua")

 

And i get this warnings: bad argument @ 'stopSound' [expected sound at argument 1, got nil] and bad argument @ 'detachElement' [expected element at argument 1, got nil] .. -.-

how to fix this?

Link to comment

Your main problem is the fact that you declare soundd as a local variable and then attempt to refer to it in another call to the function. Once the function ends, the local variables are gone and you can no longer refer to them. This is why detachElement received a nil value -- soundd was nil after the function was exited.

addEventHandler("khifizene", root, 
  function()
    if isElement(soundd) then stopSound(soundd) return end --if the soundd still exists, destroy it (no need to detach if it's getting destroyed) and exit the function without creating a new one (so it works like a toggle - creates sound when there is none, destroys it if there is one)
    local x, y, z = getElementPosition(localPlayer) 
    soundd = playSound3D(streamURL, x, y, z, true) --don't do this local otherwise you won't be able to refer to it after you exit out of the function
    setSoundVolume(soundd , volume) 
    setSoundMaxDistance(soundd , radius) 
    attachElements(soundd, localPlayer)
  end
)

 

Edited by MrTasty
  • Thanks 1
Link to comment
18 minutes ago, MrTasty said:

Your main problem is the fact that you declare soundd as a local variable and then attempt to refer to it in another call to the function. Once the function ends, the local variables are gone and you can no longer refer to them. This is why detachElement received a nil value -- soundd was nil after the function was exited.


addEventHandler("khifizene", root, 
  function()
    if isElement(soundd) then stopSound(soundd) return end --if the soundd still exists, destroy it (no need to detach if it's getting destroyed) and exit the function without creating a new one (so it works like a toggle - creates sound when there is none, destroys it if there is one)
    local x, y, z = getElementPosition(localPlayer) 
    soundd = playSound3D(streamURL, x, y, z, true) --don't do this local otherwise you won't be able to refer to it after you exit out of the function
    setSoundVolume(soundd , volume) 
    setSoundMaxDistance(soundd , radius) 
    attachElements(soundd, localPlayer)
  end
)

 

Thank you!:D

Link to comment

Using checks usually helps, like shown in the code says if isElement() then
That's a check, which prevents errors in your code. Using this you know that if the script does nothing in a result, that either the sound does not exist, is not an element, or is not visible to the other function. I hope that this helps, have a nice day.

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