Jump to content

Car Lock System


koragg

Recommended Posts

I made a nice car lock system (with sounds) but got one question. How to make it so that only players near me would hear the sounds? Put in another way, how to trigger the client event which plays the lock/unlock sounds just to those players that are near me at the moment of activating the script?

Client:

addEvent("alarmOnSound", true)
function alarmOnSound()
	playSound("lock.mp3")
end
addEventHandler("alarmOnSound", localPlayer, alarmOnSound)
---------------------------------------------------------------------------------------------------------------------------
addEvent("alarmOffSound", true)
function alarmOffSound()
	playSound("unlock.mp3")
end
addEventHandler("alarmOffSound", localPlayer, alarmOffSound)

Server:

function alarmToggle(player)
	local car = getPedOccupiedVehicle(player)
	if car then
		if isVehicleLocked(car) then
			setVehicleLocked(car, false)
			triggerClientEvent(root, "alarmOffSound", player)
			setVehicleOverrideLights(car, 2)
			setTimer(function(car)
				setVehicleOverrideLights(car, 1)
			end, 300, 1, car)
		else
			setVehicleLocked(car, true)
			triggerClientEvent(root, "alarmOnSound", player)
			setVehicleOverrideLights(car, 2)
			setTimer(function(car)
				setVehicleOverrideLights(car, 1)
			end, 300, 1, car)
		end
	end
end
---------------------------------------------------------------------------------------------------------------------------
function bindKeyOnJoin()
	bindKey(source, "K", "down", alarmToggle)
end
addEventHandler("onPlayerJoin", root, bindKeyOnJoin)
---------------------------------------------------------------------------------------------------------------------------
function bindKeyOnResStart()
	for i, player in ipairs(getElementsByType("player")) do
		bindKey(player, "K", "down", alarmToggle)
	end
end
addEventHandler("onResourceStart", root, bindKeyOnResStart)

Also if I'm not spawned I shouldn't be able to activate any sounds nor lock my vehicle.

Edited by koragg
Link to comment

Like this?

function alarmToggle(player)
	local car = getPedOccupiedVehicle(player)
	local x, y, z = getElementPosition(player)
	local soundSphere = createColSphere(x, y, z, 100)
	local playersWhichHear = getElementsWithinColShape(soundSphere, "player")
	if car then
		if isVehicleLocked(car) then
			setVehicleLocked(car, false)
			triggerClientEvent(playersWhichHear, "alarmOffSound", player)
			setVehicleOverrideLights(car, 2)
			setTimer(function(car)
				setVehicleOverrideLights(car, 1)
			end, 300, 1, car)
		else
			setVehicleLocked(car, true)
			triggerClientEvent(playersWhichHear, "alarmOnSound", player)
			setVehicleOverrideLights(car, 2)
			setTimer(function(car)
				setVehicleOverrideLights(car, 1)
			end, 300, 1, car)
		end
	end
end
---------------------------------------------------------------------------------------------------------------------------
function bindKeyOnJoin()
	bindKey(source, "K", "down", alarmToggle)
end
addEventHandler("onPlayerJoin", root, bindKeyOnJoin)
---------------------------------------------------------------------------------------------------------------------------
function bindKeyOnResStart()
	for i, player in ipairs(getElementsByType("player")) do
		bindKey(player, "K", "down", alarmToggle)
	end
end
addEventHandler("onResourceStart", root, bindKeyOnResStart)

 

Link to comment
1 minute ago, pa3ck said:

That should work yea, but 100 is way too high, play around with it, use lower numbers.

Sure. Btw, is this distance same as the one returned from getDistanceBetweenPoints3D ?

I mean I kinda know what 50 is in a script which uses getDistanceBetweenPoints3D, if I set 50 here as well, radius will be same?

Edited by koragg
Link to comment

getDistanceBetweenPoints3D works just fine.

playSound3D plays the sound at the location.

addEvent("alarmOnSound", true)
function alarmOnSound()
	local x,y,z = getElementPosition(source)
	local x2,y2,z2 = getElementPosition( localPlayer )
	if getDistanceBetweenPoints3D( x,y,z,x2,y2,z2 ) <= 20 then
		playSound3D("lock.mp3",x,y,z)
	end
end
addEventHandler("alarmOnSound", root, alarmOnSound)
---------------------------------------------------------------------------------------------------------------------------
addEvent("alarmOffSound", true)
function alarmOffSound()
	local x,y,z = getElementPosition(source)
	local x2,y2,z2 = getElementPosition( localPlayer )
	if getDistanceBetweenPoints3D( x,y,z,x2,y2,z2 ) <= 20 then
		playSound3D("unlock.mp3",x,y,z)
	end
end
addEventHandler("alarmOffSound", root, alarmOffSound)

 

  • Like 1
Link to comment
3 hours ago, koragg said:

Sure. Btw, is this distance same as the one returned from getDistanceBetweenPoints3D ?

I mean I kinda know what 50 is in a script which uses getDistanceBetweenPoints3D, if I set 50 here as well, radius will be same?

I'm not sure about that, but I imagine they are the same, yea. 

 

1 hour ago, Mr.Loki said:

getDistanceBetweenPoints3D works just fine.

playSound3D plays the sound at the location.


addEvent("alarmOnSound", true)
function alarmOnSound()
	local x,y,z = getElementPosition(source)
	local x2,y2,z2 = getElementPosition( localPlayer )
	if getDistanceBetweenPoints3D( x,y,z,x2,y2,z2 ) <= 20 then
		playSound3D("lock.mp3",x,y,z)
	end
end
addEventHandler("alarmOnSound", root, alarmOnSound)
---------------------------------------------------------------------------------------------------------------------------
addEvent("alarmOffSound", true)
function alarmOffSound()
	local x,y,z = getElementPosition(source)
	local x2,y2,z2 = getElementPosition( localPlayer )
	if getDistanceBetweenPoints3D( x,y,z,x2,y2,z2 ) <= 20 then
		playSound3D("unlock.mp3",x,y,z)
	end
end
addEventHandler("alarmOffSound", root, alarmOffSound)

 

I think that's just waste of bandwidth, why would you trigger to 50 other players even though you know they will be outside the "if statement" anyway, but that's just my opinion.

  • Like 1
Link to comment

@Mr.Loki I was just asking if the returned distance is the same from the two functions. But you just made a good point. If I use getDistanceBetweenPoints3D the sound volume will fade away if the player who activates the alarm goes far away from me but still within the radius. No idea if the way I made it it would do the same. Gotta try both codes soon.

Link to comment
10 minutes ago, koragg said:

@Mr.Loki I was just asking if the returned distance is the same from the two functions. But you just made a good point. If I use getDistanceBetweenPoints3D the sound volume will fade away if the player who activates the alarm goes far away from me but still within the radius. No idea if the way I made it it would do the same. Gotta try both codes soon.

Ok and I saw that you were using playSound which plays at the same volume no matter the distance

52 minutes ago, pa3ck said:

I think that's just waste of bandwidth, why would you trigger to 50 other players even though you know they will be outside the "if statement" anyway, but that's just my opinion.

 

I didn't really think of it that way thx for tip lol

  • Like 1
Link to comment
44 minutes ago, koragg said:

@Mr.Loki I was just asking if the returned distance is the same from the two functions. But you just made a good point. If I use getDistanceBetweenPoints3D the sound volume will fade away if the player who activates the alarm goes far away from me but still within the radius. No idea if the way I made it it would do the same. Gotta try both codes soon.

As long as you have playSound3D as @Mr.Loki mentioned, it will fade away gradually when you move away from the position of the sound whether you use the getDistanceBetweenPoints3D or not

Link to comment

server

function alarmToggle(player)
	local car = getPedOccupiedVehicle(player)
	local x, y, z = getElementPosition(player)
	local soundSphere = createColSphere(x, y, z, 50)
	local playersWhichHear = getElementsWithinColShape(soundSphere, "player")
	if car then
		if isVehicleLocked(car) then
			setVehicleLocked(car, false)
			triggerClientEvent(playersWhichHear, "alarmOffSound", player)
			setVehicleOverrideLights(car, 2)
			setTimer(function(car)
				setVehicleOverrideLights(car, 1)
			end, 300, 1, car)
		else
			setVehicleLocked(car, true)
			triggerClientEvent(playersWhichHear, "alarmOnSound", player)
			setVehicleOverrideLights(car, 2)
			setTimer(function(car)
				setVehicleOverrideLights(car, 1)
			end, 300, 1, car)
		end
	end
end
---------------------------------------------------------------------------------------------------------------------------
function bindKeyOnJoin()
	bindKey(source, "K", "down", alarmToggle)
end
addEventHandler("onPlayerJoin", root, bindKeyOnJoin)
---------------------------------------------------------------------------------------------------------------------------
function bindKeyOnResStart()
	for i, player in ipairs(getElementsByType("player")) do
		bindKey(player, "K", "down", alarmToggle)
	end
end
addEventHandler("onResourceStart", root, bindKeyOnResStart)

client

addEvent("alarmOnSound", true)
function alarmOnSound()
	local x,y,z = getElementPosition(source)
	playSound3D("lock.mp3",x,y,z)
end
addEventHandler("alarmOnSound", localPlayer, alarmOnSound)
---------------------------------------------------------------------------------------------------------------------------
addEvent("alarmOffSound", true)
function alarmOffSound()
	local x,y,z = getElementPosition(source)
	playSound3D("unlock.mp3",x,y,z)
end
addEventHandler("alarmOffSound", localPlayer, alarmOffSound)

Lights flash but others don't hear the lock/unlock sound. No errors whatsoever, hmm...

Link to comment

@MIKI785 when I put 'resourceRoot' there's no sound but 'root' works just fine. Tested and now it's heard. Combined everything and now works :) Thanks fellas!

client

addEvent("alarmOnSound", true)
function alarmOnSound(car)
	local x, y, z = getElementPosition(car)
	local lockSound = playSound3D("lock.mp3", x, y, z)
	attachElements(lockSound, car)
	setSoundMaxDistance(lockSound, 50)
end
addEventHandler("alarmOnSound", root, alarmOnSound)
---------------------------------------------------------------------------------------------------------------------------
addEvent("alarmOffSound", true)
function alarmOffSound(car)
	local x,y,z = getElementPosition(car)
	local unlockSound = playSound3D("unlock.mp3", x, y, z)
	attachElements(unlockSound, car)
	setSoundMaxDistance(unlockSound, 50)
end
addEventHandler("alarmOffSound", root, alarmOffSound)

server

function alarmToggle(player)
	local state = getElementData(player, "player state")
	if state ~= "Alive" then
		return
	end
	local car = getPedOccupiedVehicle(player)
	if car then
		local x, y, z = getElementPosition(car)
		local soundSphere = createColSphere(x, y, z, 50)
		local playersWhichHear = getElementsWithinColShape(soundSphere, "player")
		if getElementData(player, "locked") == 1 then
			triggerClientEvent(playersWhichHear, "alarmOffSound", player, car)
			setVehicleOverrideLights(car, 2)
			setTimer(function(car)
				setVehicleOverrideLights(car, 1)
				setTimer(function(car)
					setVehicleOverrideLights(car, 2)
					setTimer(function(car)
						setVehicleOverrideLights(car, 1)
					end, 200, 1, car)
				end, 200, 1, car)
			end, 200, 1, car)
			setElementData(player, "locked", 0)
			return
		end
		if getElementData(player, "locked") == 0 then
			triggerClientEvent(playersWhichHear, "alarmOnSound", player, car)
			setVehicleOverrideLights(car, 2)
			setTimer(function(car)
				setVehicleOverrideLights(car, 1)
			end, 200, 1, car)
			setElementData(player, "locked", 1)
			return
		end
	end
end
---------------------------------------------------------------------------------------------------------------------------
function bindKeyOnJoin()
	bindKey(source, "J", "down", alarmToggle)
	setElementData(source, "locked", 0)
end
addEventHandler("onPlayerJoin", root, bindKeyOnJoin)
---------------------------------------------------------------------------------------------------------------------------
function bindKeyOnResStart()
	for i, player in ipairs(getElementsByType("player")) do
		bindKey(player, "J", "down", alarmToggle)
		setElementData(player, "locked", 0)
	end
end
addEventHandler("onResourceStart", resourceRoot, bindKeyOnResStart)

Would be good if it's heard while you're spectating the player who activated it but I guess that's not possible because of the colSphere.

Edited by koragg
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...