Jump to content

Random playSound


berry172

Recommended Posts

Posted (edited)

This should work unless I made some kind of weird mistake.

Remember to add the sound files in your meta.xml file and make sure the path is correct with the table's paths.

Client-side

local sounds = {"sounds/1.wav", "sounds/2.wav"} 
  
addEvent("onPlayerTrueJoin", true) 
addEventHandler("onPlayerTrueJoin", root, 
    function() 
         playSound(sounds[math.random(1, #sounds)], false) 
    end 
) 

Server-side

addEventHandler("onPlayerJoin", root, 
     function() 
          triggerClientEvent(source, "onPlayerTrueJoin", source) 
     end 
) 

Edited by Guest
Guest Guest4401
Posted
This should work unless I made some kind of weird mistake.
Yes you made 2 weird mistakes.

1. You forgot to read the first sentence of wiki in onClientPlayerJoin:

This event is triggered when a player joins a server. It is triggered for all players except the local player, as the local player joins the server before their client-side resources are started. It would also be possible for two players to join within a few seconds of each other and for the two players' scripts may not receive onClientPlayerJoin events as their scripts wouldn't have started yet.

2.

playSound(math.random(1, #sounds), false) 

should be

playSound(sounds[math.random(1, #sounds)], false) 

Guest Guest4401
Posted
Correct there, I'll update my code.

Use onClientResourceStart. Because it will say Event not added clientside if you're using onPlayerJoin event to trigger something (If the files aren't downloaded)

local sounds = {"sounds/1.wav", "sounds/2.wav"} 
  
addEventHandler("onClientResourceStart", resourceRoot, 
    function() 
         playSound(sounds[math.random(1, #sounds)], false) 
    end 
) 

Posted
Correct there, I'll update my code.

Use onClientResourceStart. Because it will say Event not added clientside if you're using onPlayerJoin event to trigger something (If the files aren't downloaded)

local sounds = {"sounds/1.wav", "sounds/2.wav"} 
  
addEventHandler("onClientResourceStart", resourceRoot, 
    function() 
         playSound(sounds[math.random(1, #sounds)], false) 
    end 
) 

how to url playsound?

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