Jump to content

Can't trigger server event


spaghetti1337

Recommended Posts

I'm trying to trigger server event:
 

---server side ---

function removePlayerVehicle(player, vehicle)
    local playerID 
    if isElement(player) then
        playerID = getElementData(player, "acc:id")
    elseif tonumber(player) then
        playerID = tonumber(player)
    end
    
    local vehicleID
    if isElement(vehicle) then
        vehicleID = getElementData(vehicle, "veh:id")
    elseif tonumber(vehicle) then
        vehicleID = tonumber(vehicle)
    end
    
    if playerID >= 1 then
        if ownerCache[playerID] then
            local data = ownerCache[playerID]
            for k,v in pairs(data) do
                if v == vehicleID then
                    table.remove(ownerCache[playerID], k)
                    return true
                end
            end
        end
    end
        
    return false
end
addEvent("removePlayerVehicle", true)
addEventHandler("removePlayerVehicle", root, removePlayerVehicle)

--- client side ---

triggerServerEvent("removePlayerVehicle", localPlayer, vehicle)

Error: Client (player) triggered serverside event removePlayerVehicle, but event is not added serverside

Also I tried to replace triggerServerEvent with exports:

exports['vehicle']:removePlayerVehicle(localPlayer, vehicle)

--- meta.xml ---
 

<export function="removePlayerVehicle" type="shared"></export>

Error: call: failed to call 'vehicle:removePlayerVehicle' [string "?"]

Link to comment

1) Is there any error inside the server or client debug console when starting your resource?

2) What is the content of your meta.xml file for the triggerServerEvent version?

NOTE: shared does not mean synchronized, but that the export rule is applied to both clientside and serverside or the script is available on both ends.

Edited by The_GTA
Link to comment
1 minute ago, The_GTA said:

1) Is there any error inside the client debug console when starting your resource?

2) What is the content of your meta.xml file for the triggerServerEvent part?

NOTE: shared does not mean synchronized, but that the export rule is applied to both clientside and serverside or the script is available on both ends.

1, no errors

2, 

<script src="server.lua" type="server" cache="false"></script>

 

Link to comment
1 hour ago, spaghetti1337 said:

1, no errors

2, 

<script src="server.lua" type="server" cache="false"></script>

 

This issue is really strange. I doubt that the client script is starting faster than the serverside script. Could you send me the complete resource so that I could take a look?

Link to comment
1 hour ago, The_GTA said:

This issue is really strange. I doubt that the client script is starting faster than the serverside script. Could you send me the complete resource so that I could take a look?

I re-created the resource and now works fine but I still don't know what was the problem... but at least it works now, lol - but thanks your replies and time again 

Edited by spaghetti1337
  • Like 1
Link to comment
  • Moderators
2 hours ago, The_GTA said:

This issue is really strange.

I had a similar issue on my local machine in the past. For some unknown reason the server wasn't ready yet to receive the event, when the client triggered a server event immediately. Not sure if that is going on with this script but clientside looks kinda empty. So I wouldn't consider it to be impossible.

 

 

 

  • Like 1
Link to comment

That looks unusual. While it may look intuitive that the script will execute on server before executing on client, I guess there's no guarantee it will be that way, so it looks like a race condition. But I would expect onClientResourceStart to get triggered after everything has been set up on the server so it's better to do this kind of communication from that event.

  • Like 1
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...