How to get "StartAlarm" to trigger for everyone?

Hi, i’m new to coding and was wondering if anyone could help me along. I currently have a clientside script that triggers “StartAlarm” via a command, but the problem is the “StartAlarm” only triggers for the person that used the command, so I was wondering how could I make it trigger for all players. I know I have to do something a with a server.lua, but honestly no idea what. Thanks!

My current client.lua

-- Start Alarm
RegisterCommand('startalarm', function(source, args)
    TriggerEvent('chat:addMessage', {
        args = { 'Prison Alarm Activated!' }
    })
	
	while not PrepareAlarm("PRISON_ALARMS") do
		Citizen.Wait(0)
	end
	StartAlarm("PRISON_ALARMS", 0)
	
end, false)


-- Stop Alarm
RegisterCommand('stopalarm', function(source, args)
    TriggerEvent('chat:addMessage', {
        args = { 'Prison Alarm Stopped!' }
    })
	StopAlarm("PRISON_ALARMS", 1)
end, false)

Try doing it like this with server.lua

Server.lua

-- Start Alarm
RegisterCommand('startalarm', function(source, args)
    TriggerClientEvent('chat:addMessage', -1, { args = { '^1SYSTEM', Prison Alarm Activated!' } })
    TriggerEvent("alarmcentral:startalarmCL",-1)
end, false)

-- Stop Alarm
RegisterCommand('stopalarm', function(source, args)
TriggerClientEvent('chat:addMessage', -1, { args = { '^1SYSTEM', Prison Alarm Deactivated!' } })
TriggerEvent("alarmcentral:stopalarmCL",-1)
end, false)
RegisterServerEvent("alarmcentral:startalarmSV")
AddEventHandler('alarmcentral:startalarmSV', function()
TriggerClientEvent("alarmcentral:startalarmCL", -1)
end)
RegisterServerEvent("alarmcentral:stopalarmSV")
AddEventHandler('alarmcentral:stopalarmSV', function()
TriggerClientEvent("alarmcentral:stopalarmCL", -1)
end)

and then in a client.lua

Client.lua

RegisterNetEvent("alarmcentral:startalarmCL")
AddEventHandler('alarmcentral:startalarmCL', function()
while not PrepareAlarm("PRISON_ALARMS") do
		Citizen.Wait(0)
	end
	StartAlarm("PRISON_ALARMS", 0)
end)
RegisterNetEvent("alarmcentral:stopalarmCL")
AddEventHandler('alarmcentral:stopalarmCL', function()
StopAlarm("PRISON_ALARMS", 1)
end)

That was just off the top of my head writing that so if try it and test it and see if it works for you. I haven’t tested it myself just wrote it from off the top of my head so that should help you and remember for future reference when you pass -1 to any Event as such TriggerClientEvent(“alarmcentral:stopalarmCL”, -1) the -1 means it will trigger on everyone in the server.

does it work for banks ??? if I replace with Bank Alarm? could this be done ?? :heart_eyes: :heart_eyes: :heart_eyes:

yes but you would have to find the line for the bank alarm though, I am not looking it up for you, that is something I would hope you would know where to find the bank alarm string at to replace “PRISON_ALARMS” string with whatever the bank alarm one is.

a little idea as for example the fleeca bank I should put " Nordea Alarm" … :stuck_out_tongue:

Thanks, you forgot the to add the starting ’ to line 3 of { ‘^1SYSTEM’, Prison Alarm Activated!’ } } before “Prison”.
After that the code seems to work with no errors, but I get the “SYSTEM: Prison Alarm Activated!” but no alarm seems to play. (Yes i’m in the prison of course)

yes and problem with -1 and 0 for activate /desactivate i’m looking :stuck_out_tongue:

1 Like

Also, if your still looking for the list of alarm types you can find them here https://runtime.fivem.net/doc/natives/#_0x0355EF116C4C97B2

So I believe the fleeca bank in Paleto Bay would be “PALETO_BAY_SCORE_ALARM”

I thought well I’m a pro in algebra 0 it’s not a function sa must be 1 and -1

I’m not exactly sure what you mean, could you post exactly what you did? Thanks :slight_smile:

I’ve been learning coding for a week :rofl:

Error running system event handling function for resource AlarmBanque: citizen:/scripting/lua/scheduler.lua:41: Failed to execute thread: client.lua:3: attempt to call a nil value (global ‘PrepareAlarm’)
stack traceback:
client.lua:3: in upvalue ‘handler’
citizen:/scripting/lua/scheduler.lua:175: in function citizen:/scripting/lua/scheduler.lua:174
stack traceback:
[C]: in function ‘error’
citizen:/scripting/lua/scheduler.lua:41: in field ‘CreateThreadNow’
citizen:/scripting/lua/scheduler.lua:174: in function citizen:/scripting/lua/scheduler.lua:138
[C]: in function ‘coroutine.resume’
citizen:/scripting/lua/scheduler.lua:34: in field ‘CreateThreadNow’
citizen:/scripting/lua/scheduler.lua:335: in function citizen:/scripting/lua/scheduler.lua:322

cl

RegisterNetEvent("alarmcentral:startalarmCL")
AddEventHandler('alarmcentral:startalarmCL', function()
while not PrepareAlarm("PRISON_ALARMS") do
		Citizen.Wait(10)
	end
	StartAlarm("PRISON_ALARMS", 1)
end)
RegisterNetEvent("alarmcentral:stopalarmCL")
AddEventHandler('alarmcentral:stopalarmCL', function()
StopAlarm("PRISON_ALARMS", -1)
end)

Sv

-- Start Alarm
RegisterCommand('startalarm', function(source, args)
    TriggerClientEvent('chat:addMessage', 1, { args = { '^1SYSTEM', 'Prison Alarm Activated!' } })
    TriggerEvent("alarmcentral:startalarmCL",1)
end, false)

-- Stop Alarm
RegisterCommand('stopalarm', function(source, args)
TriggerClientEvent('chat:addMessage', -1, { args = { '^1SYSTEM', 'Prison Alarm Deactivated!' } })
TriggerEvent("alarmcentral:stopalarmCL",-1)
end, false)
RegisterServerEvent("alarmcentral:startalarmSV")
AddEventHandler('alarmcentral:startalarmSV', function()
TriggerClientEvent("alarmcentral:startalarmCL", 1)
end)
RegisterServerEvent("alarmcentral:stopalarmSV")
AddEventHandler('alarmcentral:stopalarmSV', function()
TriggerClientEvent("alarmcentral:stopalarmCL", -1)
end)

don’t work sorry :sweat:

Strange, not getting any errors from your code, but still no sounds from the alarm :frowning:

empty the cache :stuck_out_tongue:

I tried clearing the cache, but alas still no sound is produced, it seems like for some reason it’s not triggering the clientside portion.

I tried added a " print(“ALARM EVENT TRIGGERED”) " in the client.lua but the print doesn’t appear in F8 Console.
Client.lua so you can see what i’m talking about

RegisterNetEvent("alarmcentral:startalarmCL")
AddEventHandler('alarmcentral:startalarmCL', function()
	print("ALARM EVENT TRIGGERED")
while not PrepareAlarm("PRISON_ALARMS") do
		Citizen.Wait(0)
	end
	StartAlarm("PRISON_ALARMS", 1)
end)

RegisterNetEvent("alarmcentral:stopalarmCL")
AddEventHandler('alarmcentral:stopalarmCL', function()
StopAlarm("PRISON_ALARMS", -1)
end)

the code looks good maybe it’s the location that is wrong I put it after esx … maybe put it before …

I figured it out
in server.lua it was

TriggerEvent("alarmcentral:startalarmCL", 1)

I changed it to

TriggerEvent("alarmcentral:startalarmSV", -1)

And now it works perfectly

1 Like

put the code in full :stuck_out_tongue:

client.lua

RegisterNetEvent("alarmcentral:startalarmCL")
AddEventHandler('alarmcentral:startalarmCL', function()
while not PrepareAlarm("PRISON_ALARMS") do
		Citizen.Wait(0)
	end
	StartAlarm("PRISON_ALARMS", 1)
end)

RegisterNetEvent("alarmcentral:stopalarmCL")
AddEventHandler('alarmcentral:stopalarmCL', function()
StopAlarm("PRISON_ALARMS", -1)
end)

server.lua

-- Start Alarm
RegisterCommand('startalarm', function(source, args)
    TriggerClientEvent('chat:addMessage', 1, { args = { '^1SYSTEM', 'Prison Alarm Activated!' } })
    TriggerEvent("alarmcentral:startalarmSV", -1)
end, false)

-- Stop Alarm
RegisterCommand('stopalarm', function(source, args)
TriggerClientEvent('chat:addMessage', -1, { args = { '^1SYSTEM', 'Prison Alarm Deactivated!' } })
TriggerEvent("alarmcentral:stopalarmSV",-1)
end, false)

RegisterServerEvent("alarmcentral:startalarmSV")
AddEventHandler('alarmcentral:startalarmSV', function()
TriggerClientEvent("alarmcentral:startalarmCL", -1)
end)

RegisterServerEvent("alarmcentral:stopalarmSV")
AddEventHandler('alarmcentral:stopalarmSV', function()
TriggerClientEvent("alarmcentral:stopalarmCL", -1)
end)
1 Like

it’s really fishy, ​​to stop the alarm is ok? :smile:

Stopping the alarm seems to works fine :stuck_out_tongue:

1 Like