Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

fivem scripting I'm working on a team script I've currently got a teams table on the…

  1. User

    fivem scripting I'm working on a team script I've currently got a teams table on the server side the only way to modify the table is by triggering a server event on the client how can I make it so once a client has trigger an event for eg team A then nobody else can join that team

  2. Assistant

    To achieve this, you can modify your server-side script to keep track of the number of players in each team and prevent further players from joining once the team is full. You can follow these steps:

    1. Initialize the teams table on the server side with a count for each team.
    local teams = {
        teamA = {count = 0, maxCount = 1},
        teamB = {count = 0, maxCount = 1},
        // Add more teams if needed.
    }
    
    1. Create a function to check if a team is full.
    local function isTeamFull(teamName)
        return teams[teamName].count >= teams[teamName].maxCount
    end
    
    1. Update the server event to check if a team is full before adding a player.
    RegisterServerEvent('joinTeam')
    AddEventHandler('joinTeam', function(teamName)
        if not isTeamFull(teamName) then
            -- Add player to the team and increment the team count.
            teams[teamName].count = teams[teamName].count + 1
    
            -- Inform the player that they have joined the team.
            TriggerClientEvent('teamJoined', source, teamName)
        else
            -- Inform the player that the team is full.
            TriggerClientEvent('teamFull', source, teamName)
        end
    end)
    
    1. Handle the 'teamJoined' and 'teamFull' events on the client-side to show confirmation or rejection messages.
    RegisterNetEvent('teamJoined')
    AddEventHandler('teamJoined', function(teamName)
        print('You have joined ' .. teamName)
        -- Add any code to handle joining the team, e.g., changing player model, etc.
    end)
    
    RegisterNetEvent('teamFull')
    AddEventHandler('teamFull', function(teamName)
        print('Team ' .. teamName .. ' is full')
        -- Add any code to handle team full scenario, e.g., showing a message to the player, etc.
    end)
    

    With this implementation, once a client triggers the event for a specific team, no other player can join the team once it's full. Make sure to adjust maxCount according to the maximum number of players you want in each team.

  3. User

    what is a default fivem native i could use for a notification

  4. Assistant

    In FiveM, you can use the native ShowNotification function to display a notification on the player's screen. Here's how you can use it in combination with other natives:

    function ShowNotification(text)
        SetNotificationTextEntry("STRING")
        AddTextComponentString(text)
        DrawNotification(false, true)
    end
    

    Now, you can call the ShowNotification function with your desired text to display a notification. For instance, if you want to notify the player when they join a team, replace the print function in the 'teamJoined' event handler with the ShowNotification function:

    RegisterNetEvent('teamJoined')
    AddEventHandler('teamJoined', function(teamName)
        ShowNotification('You have joined ' .. teamName)
        -- Add any code to handle joining the team, e.g., changing player model, etc.
    end)
    
    RegisterNetEvent('teamFull')
    AddEventHandler('teamFull', function(teamName)
        ShowNotification('Team ' .. teamName .. ' is full')
        -- Add any code to handle team full scenario, e.g., showing a message to the player, etc.
    end)
    

    This will display a notification on the player's screen when they join a team or when the team is full.