Jump to content

Why?


Recommended Posts

Guess who's back.

SERVER:

function CriarPed (thePlayer, command, id, model, dimensao)
    local sX, sY, sZ = getElementPosition(thePlayer)
    ped = createPed(tonumber(model), sX, sY, sZ)
    pedID = setElementID (ped, id)
    dimensaoP = setElementDimension(ped, tonumber(dimensao))
    if pedID and dimensaoP then
    qDimensao = getElementDimension(ped)
    nomePed = getElementID (ped)
    outputChatBox (horaJ.. "Criaste um ped chamado " ..corPed.. nomePed.. corMJ.. " com o modelo " ..corPed.. model.. corMJ.." na dimensão " ..corEspaco.. tostring(qDimensao).. corMJ.. ".", thePlayer, isR, isG, isB, true)
    addEventHandler("onPedWasted", ped, pedMorreu)
    triggerClientEvent ("onPedCriado", thePlayer, ped)
    else
        destroyElement(ped)
        outputChatBox(horaE.. "Uso: /criarped [" ..corPed.. "Nome do Ped" ..corErro.. "] [" ..corPed.. "0" ..corErro.. "-" ..corPed.. "312" ..corErro.. "] [" ..corEspaco.. "0" ..corErro.. "-" ..corEspaco.. "65535" ..corErro.. "].", source, isR, isG, isB, true)
    end
end
addCommandHandler("criarped", CriarPed)
addCommandHandler("cp", CriarPed)
addCommandHandler("criarped", CriarPed)

So, in the serverside I made this script that whenever I type /criarped [ID] [model] [dimension] it creates a ped with those parameters and triggers a Client Even named "onPedCriado".

 

CLIENT

function TocarSomPed(ped)
    local pX, pY, pZ = getElementPosition(ped)
    local dimensaoP = getElementDimension(ped)
    IDPed = getElementID(ped)
    outputChatBox("O nome deste ped é " ..IDPed)
    soundREEE = playSound3D("reeet.mp3", pX, pY, pZ, true)
    setElementDimension(soundREEE, dimensaoP)
    attachElements(soundREEE, ped)
    setarIDSom = setElementID(soundREEE, IDPed)
    IDSom = getElementID(setarIDSom)
    outputChatBox("O nome deste som é " ..tostring(IDSom))
    addEventHandler("onClientPedWasted", getRootElement(), pedMorreuClient)
end
addEvent("onPedCriado", true)
addEventHandler("onPedCriado", root, TocarSomPed)


function pedMorreuClient(setarIDSom)
  	IDSound = getElementByID(setarIDSom)
    if setarIDSom and getElementByID(setarIDSom) then
    destroyElement(getElementByID(setarIDSom))
    outputChatBox("O som " ..tostring(IDSom))
    else
        outputChatBox("Erro")
    end
end

The onPedCriado event triggers the function TocarSomPed that creates a 3D Sound and attaches it to the ped. It also gets the ped ID (and correctly sends me a message with the name of the ped). Now, supposedly, after creating the 3dSound, it should get the ped ID, give that same ID  to the 3dsound and send me a message saying the ID of the sound, but instead it sends me one saying the ID of that sound is false. 

Anyways, if the ped died, it would activate the function pedMorreuClient, get the ElementByID of the sound and destroy it, but it doesn't. :/

 

Sorry, once again. And thanks for the help.

 

PS: If I just simply destroyed the Element without trying to get his ID, it would just destroy the sound of the last ped I created. If I killed one I created before the last ped, he's sound would keep playing forever.

Edited by Hugo_Almeidowski
Link to comment
  • Moderators

ID elements have to be unique. There shouldn't be two elements with the same ID.

 

local pedContainer = createElement("pedContainer")

function TocarSomPed(ped)
    local pX, pY, pZ = getElementPosition(ped)
    local dimensaoP = getElementDimension(ped)
    local IDPed = getElementID(ped)
    outputChatBox("O nome deste ped é " ..IDPed)
	
    local soundREEE = playSound3D("reeet.mp3", pX, pY, pZ, true)
	
    setElementDimension(soundREEE, dimensaoP)
    attachElements(soundREEE, ped)

	setElementParent(soundREEE, ped) -- the SOUND is now a child of the PED.
	
	setElementParent(ped, pedContainer) -- PED is now a child of pedContainer, which has an addEventHandler attached to it. See line 1 and line 31.
    
end
addEvent("onPedCriado", true)
addEventHandler("onPedCriado", root, TocarSomPed)

--

function pedMorreuClient()
	
	local setarIDSom = source -- source is the ped. See page: https://wiki.multitheftauto.com/wiki/OnClientPedWasted
	
	local soundREEE = getElementChild ( setarIDSom, 0 ) -- get the first child of this PED, which is the SOUND. See line 12.
	if soundREEE then 
		destroyElement(soundREEE)
	end
end
addEventHandler("onClientPedWasted", pedContainer, pedMorreuClient)

 

 

 

Edited by IIYAMA
Link to comment
31 minutes ago, IIYAMA said:

ID elements have to be unique. There shouldn't be two elements with the same ID.

 


local pedContainer = createElement("pedContainer")

function TocarSomPed(ped)
    local pX, pY, pZ = getElementPosition(ped)
    local dimensaoP = getElementDimension(ped)
    local IDPed = getElementID(ped)
    outputChatBox("O nome deste ped é " ..IDPed)
	
    local soundREEE = playSound3D("reeet.mp3", pX, pY, pZ, true)
	
    setElementDimension(soundREEE, dimensaoP)
    attachElements(soundREEE, ped)

	setElementParent(soundREEE, ped) -- the SOUND is now a child of the PED.
	
	setElementParent(ped, pedContainer) -- PED is now a child of pedContainer, which has an addEventHandler attached to it. See line 1 and line 31.
    
end
addEvent("onPedCriado", true)
addEventHandler("onPedCriado", root, TocarSomPed)

--

function pedMorreuClient()
	
	local setarIDSom = source -- source is the ped. See page: https://wiki.multitheftauto.com/wiki/OnClientPedWasted
	
	local soundREEE = getElementChild ( setarIDSom, 0 ) -- get the first child of this PED, which is the SOUND. See line 12.
	if soundREEE then 
		destroyElement(soundREEE)
	end
end
addEventHandler("onClientPedWasted", pedContainer, pedMorreuClient)

 

 

 

Thank you very much for all the explanation and the links!

But now the sound doesn't play when I create the ped :/ (and I don't receive the message telling me the ped's name). I tried to make it work but I couldn't

Edited by Hugo_Almeidowski
Link to comment
1 minute ago, IIYAMA said:

@Hugo_Almeidowski

Did you check the debug console? I didn't change anything before the audio started to play, so either it was already broken or there is an error/warning.

No error nor any message in the console. And I don't think there's any error in the function that triggers the client event (see below), nor in the function that makes the sound play.

function CriarPed (thePlayer, command, id, model, dimensao)
    local sX, sY, sZ = getElementPosition(thePlayer)
    ped = createPed(tonumber(model), sX, sY, sZ)
    pedID = setElementID (ped, id)
    dimensaoP = setElementDimension(ped, tonumber(dimensao))
    if pedID and dimensaoP then
    qDimensao = getElementDimension(ped)
    nomePed = getElementID (ped)
    outputChatBox (horaJ.. "Criaste um ped chamado " ..corPed.. nomePed.. corMJ.. " com o modelo " ..corPed.. model.. corMJ.." na dimensão " ..corEspaco.. tostring(qDimensao).. corMJ.. ".", thePlayer, isR, isG, isB, true)
    triggerClientEvent ("onPedCriado", thePlayer, ped)
    else
        destroyElement(ped)
        outputChatBox(horaE.. "Uso: /criarped [" ..corPed.. "Nome do Ped" ..corErro.. "] [" ..corPed.. "0" ..corErro.. "-" ..corPed.. "312" ..corErro.. "] [" ..corEspaco.. "0" ..corErro.. "-" ..corEspaco.. "65535" ..corErro.. "].", source, isR, isG, isB, true)
    end
end
addCommandHandler("criarped", CriarPed)
addCommandHandler("cp", CriarPed)
addCommandHandler("criarped", CriarPed)

It's weirds because I believe that what you've sent me is perfect... Makes no sense.

1 minute ago, Hugo_Almeidowski said:

No error nor any message in the console. And I don't think there's any error in the function that triggers the client event (see below), nor in the function that makes the sound play.


function CriarPed (thePlayer, command, id, model, dimensao)
    local sX, sY, sZ = getElementPosition(thePlayer)
    ped = createPed(tonumber(model), sX, sY, sZ)
    pedID = setElementID (ped, id)
    dimensaoP = setElementDimension(ped, tonumber(dimensao))
    if pedID and dimensaoP then
    qDimensao = getElementDimension(ped)
    nomePed = getElementID (ped)
    outputChatBox (horaJ.. "Criaste um ped chamado " ..corPed.. nomePed.. corMJ.. " com o modelo " ..corPed.. model.. corMJ.." na dimensão " ..corEspaco.. tostring(qDimensao).. corMJ.. ".", thePlayer, isR, isG, isB, true)
    triggerClientEvent ("onPedCriado", thePlayer, ped)
    else
        destroyElement(ped)
        outputChatBox(horaE.. "Uso: /criarped [" ..corPed.. "Nome do Ped" ..corErro.. "] [" ..corPed.. "0" ..corErro.. "-" ..corPed.. "312" ..corErro.. "] [" ..corEspaco.. "0" ..corErro.. "-" ..corEspaco.. "65535" ..corErro.. "].", source, isR, isG, isB, true)
    end
end
addCommandHandler("criarped", CriarPed)
addCommandHandler("cp", CriarPed)
addCommandHandler("criarped", CriarPed)

It's weirds because I believe that what you've sent me is perfect... Makes no sense.

Oh wait. I've copied it once again, but now the sound starts playing but it doesn't stop whenever the ped dies. :/ 

Link to comment
4 minutes ago, IIYAMA said:

@Hugo_Almeidowski

hmm, just to be sure debug the sound element.

 


local soundREEE = playSound3D("reeet.mp3", pX, pY, pZ, true)
outputChatBox(inspect(soundREEE))

 

It sends me this message: 

elem:sound000200C6

I don't know what that means, and I didn't know about that inspect thing, I'll study it.

 

@EDIT: everytime i create another Ped, the last digit goes up +1

 

BTW, I've edited the last message before this one, I don't know if you saw it

Edited by Hugo_Almeidowski
Link to comment
  • Moderators
Just now, Hugo_Almeidowski said:

It sends me this message: 

elem:sound000200C6

I don't know what that means, and I didn't know about that inspect thing, I'll study it.

It means that the sound has been created successfully.

 

(Are you sure that you are in the same dimension as the ped?)

 

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