Jump to content

Help with TriggerServerEvent Client/Server Not Working


Recommended Posts

Posted (edited)

Hey guys please help!!! I have been researching and researching for almost 8 hours now.  And I am just stuck as to what I am fundamentally getting wrong here.  

 

Main goal:  I would like when the person gets into a vehicle, read the license plates, send plates to the server, read a table in the DB, then send info from table back to the client to use in other functions.

What works:  I am OK server side, I am able to read from the database and pass that back to the client, but can only trigger server side with a RegisterCommand.

Issue: Client side, when I start using triggers inside of an if statement it stops working, (yes I am sure conditions are met).  I am currently only able to trigger the server function with a register command, when I try to send a trigger from the client to the server, it will not launch the server function.  

 

Working code:

client.Lua

RegisterNetEvent("servertest:output_status") --from server.Lua
AddEventHandler("servertest:output_status", function(argument)
    TriggerEvent("chatMessage", "[Success]", {0,255,0}, argument)
    TriggerEvent("servertest:vehStatus", argument)
end) 

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(1000)
        local playerPed = GetPlayerPed(-1)
        if IsPedInAnyVehicle(playerPed, false) then
            AddEventHandler("servertest:vehStatus", function(vehstatus)
                TriggerEvent("chatMessage", "[LOOP]", {0,255,0}, vehstatus)
            end) 
        end
    end
end)

 

server.Lua:

RegisterCommand("get", function(source, args) --I dont want to trigger this way, I want to trigger from client side
	MySQL.Async.fetchAll('SELECT * FROM vehicle_data WHERE plate = @plate', {
		['@plate'] = "TSTPLATE1"
	}, function(result)
		if result[1] then
		    local vehstatus = result[1].status
		    TriggerClientEvent("servertest:output_status", source, vehstatus)  
		    cb(true)
		else
		    cb(false)
		end
	end)
end)

 

What I want to do:

client.Lua:

RegisterNetEvent("servertest:output_status") --from server.Lua
AddEventHandler("servertest:output_status", function(argument)
    TriggerEvent("chatMessage", "[Success]", {0,255,0}, argument) --never receive message
    TriggerEvent("servertest:vehStatus", argument) --not used now, save for later use
end) 

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(1000)
        local playerPed = GetPlayerPed(-1)
        if IsPedInAnyVehicle(playerPed, false) then
            local playerVeh = GetVehiclePedIsIn(playerPed, false)
            local vehPlates = GetVehicleNumberPlateText(playerVeh)
            TriggerServerEvent("servertest:sendInfo", vehPlates) --this trigger never happens even though criteria is met
        end
    end
end)

 

server.Lua:

RegisterNetEvent("servertest:sendInfo")
AddEventHandler("servertest:sendInfo", function(plates)
	MySQL.Async.fetchAll('SELECT * FROM vehicle_data WHERE plate = @plate', {
		['@plate'] = plates
	}, function(result)
		if result[1] then
		    local vehstatus = result[1].status
		    TriggerClientEvent("servertest:output_status", source, vehstatus)  
		    cb(true)
		else
		    cb(false)
		end
	end)
end)

 

I guess maybe what I need to know is just how to trigger something server side from the client side without using a RegisterCommand.  I don't know how to structure it in my code, do I need to do it outside the while loop?  I've tried but haven't been successful.

Edited by senior penguin
Link to post
  • IIYAMA locked this topic
  • IIYAMA unlocked this topic

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