Help with creating a server side for my script

Hi I have an script client sided that works wll, the things is that I want to put it in order that when i put de command the picture display in all players screen on the server.

Here I put the client.lua if someone can help me to create the server.lua in order to apper the picture in all players screen,

ESX = nil
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)

AddEventHandler('onClientMapStart', function()
  Citizen.CreateThread(function()
    local display = false

  TriggerEvent('logo:display', false)
  end)
end)


RegisterCommand('logo', function(source)
TriggerEvent('logo:display', true)
end)

RegisterCommand('logoff', function(source)
  TriggerEvent('logo:display', false)
  end)

RegisterNetEvent('logo:display')
AddEventHandler('logo:display', function(value)
  SendNUIMessage({
    type = "logo",
    display = value
  })
end)


function ShowInfo(text, state)
  SetTextComponentFormat("STRING")
  AddTextComponentString(text)
  DisplayHelpTextFromStringLabel(0, state, 0, -1)
end```

Move those 2 commands in server.lua. They will look like this:

RegisterCommand('logo', function(source)
	TriggerClientEvent('logo:display', -1, true)
end)

RegisterCommand('logoff', function(source)
	TriggerClientEvent('logo:display', -1, false)
end)

And thats it, now it will show and hide for all players.

With that being the case… anyone on your server would be able to turn these on and off… which could lead to some interesting griefing. Would prob be best to make sure the person running the commands has a certain level of trust on the server before being able to run… like mod/admin/super admin.

SpikE

1 Like

all work great but thanks!!! But if I try to block the command for a job it gives me an error here I put the code im using

ESX = nil
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)

RegisterCommand('alertaverdeon', function(source)	

	PlayerData = ESX.GetPlayerData()

	if PlayerData.job.name == 'police' or PlayerData.job.name == 'sheriff' then	

	TriggerClientEvent('logo:display', -1, true)
	end
end)

RegisterCommand('alertaverdeoff', function(source)

	PlayerData = ESX.GetPlayerData()

	if PlayerData.job.name == 'police' or PlayerData.job.name == 'sheriff' then	

	TriggerClientEvent('logo:display', -1, false)
	end
end)

I want to put the command for only the police but it gives an error when trying to block the command

This should work:

ESX = nil
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)

RegisterCommand('alertaverdeon', function(source)	
	local xPlayer = ESX.GetPlayerFromId(source)

	if xPlayer.job.name == 'police' or xPlayer.job.name == 'sheriff' then	

	TriggerClientEvent('logo:display', -1, true)
	end
end)

RegisterCommand('alertaverdeoff', function(source)

	local xPlayer = ESX.GetPlayerFromId(source)

	if xPlayer.job.name == 'police' or xPlayer.job.name == 'sheriff' then	

	TriggerClientEvent('logo:display', -1, false)
	end
end)

Gives me error in server side

what error?

When I get back home I put here the error, other problem is that if I put the command the picturebonly displays for the people that is inside the server in that moment, if someone enters after the command was executed it wouldnt see the picture on his screen except if I put the command again

the error is SCRIPT ERROR: alertaverde/server.lua: 6 couldn´t get player data

If esx has been loaded which looks like it has… should be ok… but you could try setting a local to catch the source and then use it… like this

ESX = nil
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)

RegisterCommand('alertaverdeon', function(source)	
    local _source = source
	local xPlayer = ESX.GetPlayerFromId(_source)

	if xPlayer.job.name == 'police' or xPlayer.job.name == 'sheriff' then	

	TriggerClientEvent('logo:display', -1, true)
	end
end)

RegisterCommand('alertaverdeoff', function(source)
    local _source = source
	local xPlayer = ESX.GetPlayerFromId(_source)

	if xPlayer.job.name == 'police' or xPlayer.job.name == 'sheriff' then	

	TriggerClientEvent('logo:display', -1, false)
	end
end)

The script works perfectly thanks, but do you know anyway that if someone joins the server after the command has been executed for displaying the picture, the person that joins after executing the command can see the picture without having to put the command again??

You would need to setup a function for onplayerjoin that syncs the display which i would work completely different then and only use 1 command to toggle it on and off… give me a few mins and I will try and come up with something for you.

SpikE

Ok… so have a look at this code…

Server Script:

ESX = nil
local adverton = false
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)

RegisterCommand('alertaverde', function(source)	
    local _source = source
	local xPlayer = ESX.GetPlayerFromId(_source)
	if xPlayer.job.name == 'police' or xPlayer.job.name == 'sheriff' then	
        adverton = not adverton
        TriggerClientEvent('logo:display', -1, adverton)
	end
end)


RegisterServerEvent('scripnamehere:playerjoined')
AddEventHandler('scripnamehere:playerjoined', function()
    local _source = source
    local xPlayer = ESX.GetPlayerFromId(_source)
	if xPlayer.job.name == 'police' or xPlayer.job.name == 'sheriff' then	
	    TriggerClientEvent('logo:display', _source, adverton)
	end
end)

Client Script:

RegisterNetEvent('esx:playerLoaded')
AddEventHandler('esx:playerLoaded', function(xPlayer)
	TriggerServerEvent('scripnamehere:playerjoined')
end)

I have changed from 2 seperate commands to one command with a switch that turns if on/off depeding on the state… i have also added a server event that will send the status of the alert to any players that join by using the playerLoaded function from ESX. hopefully this is what you were looking for.

SpikE

Thanks a lot, I will test it now on my test server and I tell you if it works well, and again thanks a lot for your help

Works perfectly the script thanks a lot, you hae help me so much, thanks again and a thousand times more

Glad you were able to get it worked out and it works as you need it.

SpikE

Do you mind sharing your script? A lot of people here are looking for something like this.