Skip to content
The Chat Archive
gpt-4-0314· OpenAI WildChat-4.8M (AI2)

I'm working on a fivem event for some reason I can't trigger this server event -- Server…

  1. User

    I'm working on a fivem event for some reason I can't trigger this server event

    -- Server event to add players to team RegisterServerEvent("main-volleyball:server:setteam") AddEventHandler("main-volleyball:server:setteam", function(team) print('hello') for k,v in pairs(teams) do if k == team then teams[k] = source print(teams[k]) end end end)

    Client.lua

    RegisterCommand('joina', function() print('joined Team A') TriggerServerEvent("main-volleyball:server:setteam", "A") end)

  2. Assistant

    It seems like your code should work correctly. However, there are a few things you can check to make sure everything is properly set up:

    1. Ensure that both the server-side and client-side scripts are started in the server configuration file (server.cfg). You should have lines like these:
    start your_server_script
    start your_client_script
    
    1. Make sure your "teams" table is defined and has the correct keys. If it is not defined or empty, the server event may not find a matching key to set the source:
    teams = {
        ["A"] = {},
        ["B"] = {}
    }
    
    1. Use print statements to help with debugging. Add print statements in your client-side script and server-side script to see the flow of the event and identify any potential issues:

    Client.lua:

    RegisterCommand('joina', function()
        print('Client: Joining Team A')  -- Debug
        TriggerServerEvent("main-volleyball:server:setteam", "A")
    end)
    

    Server.lua:

    RegisterServerEvent("main-volleyball:server:setteam")
    AddEventHandler("main-volleyball:server:setteam", function(team)
        print('Server: Received Team Request')  -- Debug
        print('Team Selected:', team)  -- Debug
    
        for k,v in pairs(teams) do
            if k == team then
                teams[k] = source
                print('Server: Updated Team:', k, teams[k])  -- Debug
            end
        end
    end)
    

    If you still face issues, double-check the spelling of event names and variables, make sure both scripts are running, and check if the event can be triggered in another way (e.g., using another command or during another event).