Jump to content

Is not played playSound3D inside the player


CowBoy69

Recommended Posts

The essence of the script: The player enters the /say1 command and the sound is played inside the player. But when I enter this command, the sound does not play. There are no warnings and errors in the log. What could be the problem?

Script:

function say1(source)
        if (source) and x and y and z then
        local x,y,z = getElementPosition(source)
        if x and y and z then
        sound = playSound3D('say1.mp3',x,y,z)
        attachElements (sound,source)
        setSoundMaxDistance(sound,25)
end
end
end
addCommandHandler('say1',say1)


meta.xml:

<meta>
<script src="say1.lua" type="server"/>  -- tried type="client"- did not help
<file src="say1.mp3" type="server"/>  -- tried type="client" - did not help
</meta> 

P.S.: Maybe, my english is not very good...

Link to comment
3 hours ago, quindo said:

 


if (source) and x and y and z then


x,y,z aren't defined anywhere, so the function ends here

 

If I'm not mistaken, then, like, I should have done this:

function say1(source,x,y,z)
        if (source and x and y and z) then
        local x,y,z = getElementPosition(source)
        if (x and y and z) then
        sound = playSound3D('say1.mp3',x,y,z)
        attachElements (sound,source)
        setSoundMaxDistance(sound,25)
end
end
end
addCommandHandler('say1',say1,source,x,y,z)

But, anyway, the sound does not play. Can you show how to define variables x, y, z?

Link to comment
  • Moderators
local sound

function say1()
	if isElement(sound) then
		stopSound (sound)
	end

	local x, y, z = getElementPosition(localPlayer)
	sound = playSound3D('say1.mp3',x,y,z)
	setSoundMaxDistance(sound, 25)
end

addCommandHandler('say1', say1)

-----------------------------------------------------

addEventHandler("onClientRender", root,
function ()
	if isElement(sound) then
		local x,y,z = getElementPosition(localPlayer)
		setElementPosition (sound, x, y, z)
	end
end)

There is no source available in those functions.

 

Not here:

https://wiki.multitheftauto.com/wiki/AddCommandHandler

And not here:

https://wiki.multitheftauto.com/wiki/OnClientRender

  • Like 1
Link to comment
On 04.11.2017 at 21:56, IIYAMA said:

local sound

function say1()
	if isElement(sound) then
		stopSound (sound)
	end

	local x, y, z = getElementPosition(localPlayer)
	sound = playSound3D('say1.mp3',x,y,z)
	setSoundMaxDistance(sound, 25)
end

addCommandHandler('say1', say1)

-----------------------------------------------------

addEventHandler("onClientRender", root,
function ()
	if isElement(sound) then
		local x,y,z = getElementPosition(localPlayer)
		setElementPosition (sound, x, y, z)
	end
end)

There is no source available in those functions.

 

Not here:

https://wiki.multitheftauto.com/wiki/AddCommandHandler

And not here:

https://wiki.multitheftauto.com/wiki/OnClientRender

Everything works, thanks a lot. But I, the idiot, forgot to say more: it is necessary that this sound is heard by the nearest player near the one who reproduced the sound.
I tried to do the following:
Created a script say1_s.lua (server):

function say1Command ( playerSource, commandName )
    triggerClientEvent ( "onSay1", root)
end
addCommandHandler ( "say1", say1Command )

and created a script say1_c.lua (client):

local sound

function onSay1()
    if isElement(sound) then
        stopSound (sound)
    end

    local x, y, z = getElementPosition(localPlayer)
    sound = playSound3D('say1.mp3',x,y,z)
    setSoundMaxDistance(sound, 25)
end

addEventHandler("onClientRender", root,
function ()
    if isElement(sound) then
        local x,y,z = getElementPosition(localPlayer)
        setElementPosition (sound, x, y, z)
    end
end)
addEvent("onSay1",true)
addEventHandler("onSay1",localPlayer,say1)

But when the player enters /say1 the sound is heard from all the players of the server, regardless of the distance. How to fix this?

Link to comment

That is because you're getting the X, Y, Z position of localPlayer, which is basically every single player. You would want to trigger the event like: 

triggerClientEvent ( playerSource, "onSay1", root) -- server
local x, y, z = getElementPosition(source) -- client

OR

local x, y, z = getElementPosition(playerSource) -- server
triggerClientEvent ( root, "onSay1", root, x, y, z) -- server, use X, Y, Z on client side

But I think it would be more efficient if you didn't use triggerClientEvent this way. What you could do is, create a colSphere, get the list of players inside it and use triggerClientEvent for those players only. 

Link to comment
  • Moderators
local sounds = {}

function onSay1()
	if isElement(source) then
		local x, y, z = getElementPosition(source)
		local sound = playSound3D('say1.mp3',x,y,z)
		setSoundMaxDistance(sound, 25)
		sounds[#sounds + 1] = {sound = sound, player = source}
	end
end

addEvent("onSay1",true)
addEventHandler("onSay1", root, onSay1)

addEventHandler("onClientRender", root,
function ()
	if #sounds > 0 then
		for i=#sounds, 1, -1 do
			local soundData = sounds[i]
			local sound = soundData.sound
			local player = soundData.player
			if isElement(sound) and isElement(player) then
				local x, y, z = getElementPosition(player)
				setElementPosition (sound, x, y, z)
			else
				table.remove(sounds, i)
			end
		end
	end
end)
triggerClientEvent (root, "onSay1", player)

 

Something like this. (untested)

Link to comment
9 hours ago, IIYAMA said:

local sounds = {}

function onSay1()
	if isElement(source) then
		local x, y, z = getElementPosition(source)
		local sound = playSound3D('say1.mp3',x,y,z)
		setSoundMaxDistance(sound, 25)
		sounds[#sounds + 1] = {sound = sound, player = source}
	end
end

addEvent("onSay1",true)
addEventHandler("onSay1", root, onSay1)

addEventHandler("onClientRender", root,
function ()
	if #sounds > 0 then
		for i=#sounds, 1, -1 do
			local soundData = sounds[i]
			local sound = soundData.sound
			local player = soundData.player
			if isElement(sound) and isElement(player) then
				local x, y, z = getElementPosition(player)
				setElementPosition (sound, x, y, z)
			else
				table.remove(sounds, i)
			end
		end
	end
end)

triggerClientEvent (root, "onSay1", player)

 

Something like this. (untested)

WOW, AMAZING! It works! Thank you so much! No words :D
By the way, first the server script issued a warning. But, then, one of my friends showed me a mistake, and everything works! Here's how it should look right:

function say1Command ( playerSource, commandName )
    triggerClientEvent (root, "onSay1", playerSource)
end
addCommandHandler ( "say1", say1Command )


[The topic is closed, thank you very much]

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