Jump to content

[Question] Stopping playSound on login


kieran

Recommended Posts

Posted

Hello, I made a simple script to play a sound when my player renders, but I want to stop the sound when the player logs in and can't find out how.

Client

function PlayLoginSound()
	local sound = playSound("sounds/2.mp3", true) --Play wasted.mp3 from the sounds folder
	if (sound) then
	setSoundVolume(sound, 0.5) -- set the sound volume to 50%
		else 
		return
	end
end

addEventHandler ( "onClientResourceStart", root, PlayLoginSound)


function StopLoginSound()
	stopSound(sound)
end

addEvent("stoploginsound", true)
addEventHandler("stoploginsound", getRootElement(), StopLoginSound)

Server

--Stop login sound
function PlayLogin()
	triggerClientEvent(source, "stoploginsound")
end
addEventHandler("onPlayerLogin", root, PlayLogin)

Any help would be nice, tried triggering client event from server when player logs in, but keeps playing :/ even if it's not looped...

Posted

local variables aren't accessible outside the function. Also, replace 'root' with 'resourceRoot' in event "onClientResourceStart".

Posted
20 minutes ago, DNL291 said:

local variables aren't accessible outside the function. Also, replace 'root' with 'resourceRoot' in event "onClientResourceStart".

So just remove the local from when I create the sound or put the local variable outside the function to allow me to stop it with the triggerClientEvent?

Posted

This has been solved, it was rather simple actually :/

This:

addEventHandler("stoploginsound", getRootElement(), StopLoginSound)

Should of been this:

addEventHandler("stoploginsound", root, StopLoginSound)

 

And finally for my server side code, this:

triggerClientEvent(source, "stoploginsound")

Should of been this:

triggerClientEvent(source, "stoploginsound", source)

 

Hope this will help people that struggle with stopSound.

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