esx-framework / esx_core

Official Repo For core resources for esx-legacy
https://documentation.esx-framework.org/
GNU General Public License v3.0
356 stars 737 forks source link

[Bug] - es_extended - KeyMapping interfere with es_extended when it shouldn't #1344

Closed s1nyx closed 2 months ago

s1nyx commented 3 months ago

Hi,

Describe the bug If I try to create a KeyMapping on another script, when pressing the key it will show the message error 'Invalid Command - /cmd'.

To Reproduce Steps to reproduce the behavior: Create a client-side script with for example:

local handsUp = false
CreateThread(function()
    while true do
        Wait(0)
        if handsUp then
            TaskHandsUp(PlayerPedId(), 250, PlayerPedId(), -1, true)
        end
    end
end)
RegisterCommand('+handsup', function()
    handsUp = true
end, false)
RegisterCommand('-handsup', function()
    handsUp = false
end, false)

RegisterKeyMapping('+handsup', 'Hands Up', 'keyboard', 'i')

Expected behavior Nothing, shouldn't interfere with other scripts.

Screenshots

image

Debug Info (please complete the following information):

Additional context

This issue is from this part of the code located in es_extended/server/main.lua.

AddEventHandler("chatMessage", function(playerId, _, message)
    local xPlayer = ESX.GetPlayerFromId(playerId)
    if message:sub(1, 1) == "/" and playerId > 0 then
        CancelEvent()
        local commandName = message:sub(1):gmatch("%w+")()
        xPlayer.showNotification(TranslateCap("commanderror_invalidcommand", commandName))
    end
end)
Gellipapa commented 3 months ago

@s1nyx Hi! I copied your code into a plain client-side lua file and didn't experience any problems, the chatMessage is only called when you actually try to call a non-valid command, otherwise it is not called.

For me your code worked flawlessly, it up the hand nicely then down the hand I didn't get any errors.

s1nyx commented 3 months ago

@s1nyx Hi! I copied your code into a plain client-side lua file and didn't experience any problems, the chatMessage is only called when you actually try to call a non-valid command, otherwise it is not called.

For me your code worked flawlessly, it up the hand nicely then down the hand I didn't get any errors.

Hi,

Let me check again. I'll get back to you asap.

Gellipapa commented 2 months ago

Hi! @s1nyx Any news?

s1nyx commented 2 months ago

Hi! @s1nyx Any news? Hi,

The issue came from the fact that I didn't register the command with the -, only the +. Now fixed and works perfectly.

Have a nice day.