Jump to content

Attaching 3D Sound to Certain Player in Race


MilesFox92

Recommended Posts

Hello everyone. I've made a race map that gives a player random vehicle each time he gets to a checkpoint, and I am trying to create a script, that makes custom music play near certain player (it should also be heard by other players when near), if he gets Truck after collecting the checkpoint. And I can't seem to make music play only near the player with truck. How do I getElementPosition of the player, who got the truck?


 

Link to comment
  • Scripting Moderators

?. I think the best solution would be to use onPlayerReachCheckpoint server-side event, which is triggered when a player reaches a checkpoint.
Example:

addEventHandler ( "onPlayerReachCheckpoint", root,
	function ( checkpoint, timePassed )
		local vehicle = getPedOccupiedVehicle ( source )
		
		if not ( vehicle ) then
			return
		end
		
		if getElementModel ( vehicle ) ~= 403 then -- Linerunner
			return
		end
		
		-- do whatever you want
	end
)

 

  • Thanks 1
Link to comment

That's way more optimized, than the way I've tried to do it, thanks!
Also, I have a problems with making attached sound be heard by everyone. I only managed to make it either play for every player at once, or just for the truck player. What's the correct way of playing the 3D sound and attaching it to the certain player? I'm really inexperienced with client-sided scripts yet ?

Oh, and what do "checkpoint, timePassed" in fuction's brackets do? I couldn't find any info on that, but saw things like these in many scripts, even though all the mini-scripts I wrote were working with just "()" near function 

 

Link to comment
  • Scripting Moderators

You need to trigger an event to all clients or to those near the desired player.
Something like this:

-- Server:
addEventHandler ( "onPlayerReachCheckpoint", root,
	function ( checkpoint, timePassed )
		local vehicle = getPedOccupiedVehicle ( source )
		
		if not ( vehicle ) then
			return
		end
		
		if getElementModel ( vehicle ) ~= 403 then -- Linerunner
			return
		end
		
		local x, y, z = getElementPosition ( source )
		local nearbyPlayers = getElementsWithinRange ( x, y, z, 50, "player" )
		
		triggerClientEvent ( nearbyPlayers, "playSyncedSound", source ) -- source is the element that is the source of this event
	end
)

-- Client:
addEvent ( "playSyncedSound", true )
addEventHandler ( "playSyncedSound", root,
	function ( )
		local x, y, z = getElementPosition ( source )
		local sound = playSound3D ( "path/to/audio/file.ext", x, y, z )
		
		if ( sound ) then
			attachElements ( sound, source )
		end
	end
)
 

 

On 23/09/2022 at 22:04, lMilesl said:

Oh, and what do "checkpoint, timePassed" in fuction's brackets do?

These are just parameters that almost every event has. The description can be found on the wiki or by looking at the source code of the resource, as in this case.
The name of the parameters does not matter, only the order of the parameters is important. You don't have to specify the parameters if you don't plan to use them in your code.

  • Thanks 1
Link to comment
  • Scripting Moderators
On 27/09/2022 at 01:37, lMilesl said:

Oh, and one last question - how do I stop the music (destroy it's element I suppose) if after collecting next checkpoint player gets something, that is not a truck anymore? I've been trying to code this in different ways, but can't seem to succeed :/

try this instead of destroying https://wiki.multitheftauto.com/wiki/SetSoundPaused

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