#Remove player mask

1 messages · Page 1 of 1 (latest)

craggy oasis
#

ox_targer/server/main.lua

RegisterNetEvent("custom:removeMask", function(targetId)
    TriggerClientEvent("custom:removeMaskClient", targetId)
end)

ox_targer/client/main.lua

RegisterNetEvent("custom:removeMaskClient", function()
    local playerPed = PlayerPedId()

    -- 获取面具组件的绘制索引
    local drawable = GetPedDrawableVariation(playerPed, 1)
    
    if drawable ~= 0 then
        -- 移除面具
        SetPedComponentVariation(playerPed, 1, 0, 0, 0)
        lib.notify({title = "面具變更", description = "你的面具被摘掉了!", type = "info"})
    else
        lib.notify({title = "錯誤", description = "你沒有佩戴面具!", type = "error"})
    end
end)

ox_targer/client/default.lua

api.addGlobalPlayer({
    {
        name = "removeMask",
        label = "摘下面具",
        icon = "fas fa-mask",
        distance = 2.0,
        onSelect = function(data)
            local targetId = GetPlayerServerId(NetworkGetPlayerIndexFromPed(data.entity))
            TriggerServerEvent("custom:removeMask", targetId)
        end
    }
})
craggy oasis
#

Can help me achieve
When player A is stripped of his mask by player B, is it possible to put the mask in player B's backpack and take it out to use it?

visual field
#

Assuming you're not already using a 'clothing as item' resource; add an item with metadata to the target players inventory.

craggy oasis
#

I can get the mask item normally, but there are two small problems:

  1. The item is for the player to get it himself instead of the player who takes off the mask
  2. When the mask is taken off and put on again, it will not be preserved in the original style when it was taken off, but will be fixed to No. 50. I don't know how to do this.
#

ox-target/client/main.lua

RegisterNetEvent("custom:removeMaskClient", function()
    local playerPed = PlayerPedId()

    -- 獲取面具的繪製索引和紋理
    local drawable = GetPedDrawableVariation(playerPed, 1)
    local texture = GetPedTextureVariation(playerPed, 1)

    if drawable ~= 0 then
        -- 移除面具
        SetPedComponentVariation(playerPed, 1, 0, 0, 0)
        lib.notify({title = "面具變更", description = "你的面具被摘掉了!", type = "info"})
    else
        lib.notify({title = "錯誤", description = "你沒有佩戴面具!", type = "error"})
    end
end)

RegisterNetEvent("custom:wearMask", function()
    local playerPed = PlayerPedId()

    -- 設定面具(這裡用 50 作為範例,可根據實際需求調整)
    SetPedComponentVariation(playerPed, 1, 50, 0, 0)
    lib.notify({title = "面具變更", description = "你戴上了面具!", type = "info"})

    -- 移除物品
    TriggerServerEvent("custom:removeMaskItem")
end)
#

ox-target/server/main.lua

local QBCore = exports['qb-core']:GetCoreObject()

QBCore.Functions.CreateUseableItem("mask_item", function(source, item)
    TriggerClientEvent("custom:wearMask", source)
end)
RegisterNetEvent("custom:removeMask", function(targetId)
    local source = targetId  -- 獲取玩家 ID
    local xPlayer = QBCore.Functions.GetPlayer(source)

    if xPlayer then
        -- 給予玩家 "mask_item"
        exports.ox_inventory:AddItem(source, "mask_item", 1)
    end

    -- 觸發客戶端事件,摘下面具
    TriggerClientEvent("custom:removeMaskClient", targetId)
end)

RegisterNetEvent("custom:removeMaskItem", function()
    local source = source
    local xPlayer = QBCore.Functions.GetPlayer(source)

    if xPlayer then
        exports.ox_inventory:RemoveItem(source, "mask_item", 1)
    end
end)
#

It seems that a lot of problems have been detected
3. When you take off the player's mask again (the player should not be wearing a mask), you can still get the item

visual field
#
  1. You need to add the item to the correct player.
  2. You need to get and then assign the correct drawable.
#

You might be better off just using a clothing as item script which would handle most of this for you

craggy oasis
#

But I just want to have this function
Or do you have a clothing item script you recommend? What should I do?

stark bridge
#

I took some snippets from your code so you can see what i mean:

    local source = targetId  -- 獲取玩家 ID

        local xPlayer = QBCore.Functions.GetPlayer(source)

    if xPlayer then
        -- 給予玩家 "mask_item"
        exports.ox_inventory:AddItem(source, "mask_item", 1)
    end


craggy oasis
#

But there is still a small problem

#

About, when I use unmask command, the opponent player will take off the mask by himself.
But I want the mask taken off by the player to be placed on me (so that the police can use it as evidence)

#

this is my client.lua

local QBCore = exports['qb-core']:GetCoreObject()

RegisterCommand("mask", function()
    TriggerEvent("ayu-mask:client:ToggleMask")
end, false)


RegisterNetEvent('ayu-mask:client:ToggleMask', function()
    local ped = PlayerPedId()
    local mask = GetPedDrawableVariation(ped, 1)
    local tex = GetPedTextureVariation(ped, 1)

    if mask == 0 then
        TriggerServerEvent("ayu-mask:server:MaskOnByCommand")
    else
        TriggerEvent("ayu-mask:client:MaskOff", mask, tex)
    end
end)

RegisterNetEvent('ayu-mask:client:MaskOn', function(item)
    local ped = PlayerPedId()
    --if item.metadata.drawableId == nil then print("此面具無資料") end
    if GetPedDrawableVariation(ped, 1) ~= 0 then QBCore.Functions.Notify("你已經有面具了", "error") return end

    QBCore.Functions.Progressbar("maskon", "戴上面具..", 600, false, true, {
        disableMovement = false,
        disableCarMovement = false,
        disableMouse = false,
        disableCombat = false,
    }, {
        animDict = "mp_masks@standard_car@ds@",
        anim = "put_on_mask",
        flags = 49,
    }, {}, {}, function() -- Done
        SetPedComponentVariation(ped, 1, item.metadata.drawableId, item.metadata.textureId)
        TriggerServerEvent("ayu-mask:server:RemoveMaskItem", item)
    end, function() -- Cancel

    end)
end)

RegisterNetEvent('ayu-mask:client:MaskOff', function(mask, tex)
    local ped = PlayerPedId()
    --if GetPedDrawableVariation(ped, 1) == 0 then QBCore.Functions.Notify("你沒帶面具!", "error") return end

    QBCore.Functions.Progressbar("maskoff", "拿下面具..", 600, false, true, {
        disableMovement = false,
        disableCarMovement = false,
        disableMouse = false,
        disableCombat = false,
    }, {
        animDict = "missfbi4",
        anim = "takeoff_mask",
        flags = 49,
    }, {}, {}, function() -- Done
        SetPedComponentVariation(ped, 1, 0)
        TriggerServerEvent("ayu-mask:server:GiveMaskItem", mask, tex)
    end, function() -- Cancel

    end)
end)

RegisterNetEvent('ayu-mask:client:MaskOffA', function(targetId, commandSource)
    local ped = PlayerPedId()

    -- 获取目标玩家的面具信息
    local mask, tex = GetPedDrawableVariation(ped, 1), GetPedTextureVariation(ped, 1)

    if mask == 0 then
        QBCore.Functions.Notify("目标玩家没有面具!", "error")
        return
    end

    QBCore.Functions.Progressbar("maskoff", "拿下面具..", 600, false, true, {
        disableMovement = false,
        disableCarMovement = false,
        disableMouse = false,
        disableCombat = false,
    }, {
        animDict = "missfbi4",
        anim = "takeoff_mask",
        flags = 49,
    }, {}, {}, function() -- Done
        -- 移除目标玩家的面具
        SetPedComponentVariation(ped, 1, 0)

        -- 通过 source 获取执行命令的玩家信息
        local xPlayer = QBCore.Functions.GetPlayer(commandSource)  -- 通过 commandSource 获取玩家信息
        if xPlayer then
            -- 给执行命令的玩家添加面具物品
            TriggerServerEvent("ayu-mask:server:GiveMaskItemA", mask, tex, commandSource)
        else
            QBCore.Functions.Notify("无法获取玩家信息!", "error")
        end
    end, function() -- Cancel

    end)
end)
#

server.lua

local QBCore = exports['qb-core']:GetCoreObject()

QBCore.Functions.CreateUseableItem("mask", function(source, item)
    TriggerClientEvent("ayu-mask:client:MaskOn", source, item)
end)

RegisterNetEvent('ayu-mask:server:MaskOnByCommand', function(mask, tex)
    local src = source
    local Player = QBCore.Functions.GetPlayer(src)
    local mask = Player.Functions.GetItemByName("mask")
    if mask then
        TriggerClientEvent("ayu-mask:client:MaskOn", source, mask)
    else
        TriggerClientEvent("QBCore:Notify", src, "你沒有面具!", "error")
    end
end)

RegisterNetEvent('ayu-mask:server:GiveMaskItem', function(mask, tex)
    if mask == 0 then return end
    local src = source
    local info = {drawableId = mask, textureId = tex}

    local success = exports.ox_inventory:AddItem(src, "mask", 1, info)

    if success then
        --TriggerClientEvent('ox_lib:notify', src, { type = 'success', description = '你獲得了一個面具!' })
    else
        --TriggerClientEvent('ox_lib:notify', src, { type = 'error', description = '新增面具失敗!' })
    end
end)

RegisterNetEvent('ayu-mask:server:RemoveMaskItem', function(item)
    local src = source

    local hasItem = exports.ox_inventory:GetItem(src, item.name, nil) 

    --print("[DEBUG] hasItem:", json.encode(hasItem))

    if type(hasItem) == "table" and hasItem.count > 0 then
        local removed = exports.ox_inventory:RemoveItem(src, item.name, 1, hasItem.metadata)

        --print("[DEBUG] RemoveItem 狀態:", removed)

        if removed then
            --TriggerClientEvent('ox_lib:notify', src, { type = 'success', description = '你移除了面具!' })
        else
            --TriggerClientEvent('ox_lib:notify', src, { type = 'error', description = '移除面具失败!' })
        end
    else
        --TriggerClientEvent('ox_lib:notify', src, { type = 'error', description = '你沒有該面具!' })
    end
end)


RegisterCommand("unmask", function(source, args)
    local targetId = tonumber(args[1])  -- 获取目标玩家 ID
    if targetId then
        local xPlayer = QBCore.Functions.GetPlayer(targetId)

        if xPlayer then
            -- 触发客户端事件,让目标玩家摘下面具,同时获取执行命令玩家的信息
            TriggerClientEvent("ayu-mask:client:MaskOffA", targetId, source)  -- 传递执行命令的玩家 ID
        else
            TriggerClientEvent('QBCore:Notify', source, "目标玩家无法找到!", "error")
        end
    else
        TriggerClientEvent('QBCore:Notify', source, "请指定有效的玩家 ID!", "error")
    end
end)


RegisterNetEvent('ayu-mask:server:GiveMaskItemA', function(mask, tex, commandSource)
    if not commandSource then
        print("Command source is null!")
        return
    end

    if mask == 0 then return end
    local info = {drawableId = mask, textureId = tex}

    -- 通过 source 获取玩家对象
    local xPlayer = QBCore.Functions.GetPlayer(commandSource)
    if xPlayer then
        -- 给执行命令的玩家添加面具物品
        local success = exports.ox_inventory:AddItem(commandSource, "mask", 1, info)

        if success then
            TriggerClientEvent('ox_lib:notify', commandSource, { type = 'success', description = '你偷走了一个面具!' })
        else
            TriggerClientEvent('ox_lib:notify', commandSource, { type = 'error', description = '偷走面具失败!' })
        end
    else
        TriggerClientEvent('QBCore:Notify', commandSource, "玩家信息获取失败!", "error")
    end
end)



#

The main code that lets the player take off the mask is

client.lua

RegisterNetEvent('ayu-mask:client:MaskOffA', function(targetId, commandSource)
    local ped = PlayerPedId()

    -- 获取目标玩家的面具信息
    local mask, tex = GetPedDrawableVariation(ped, 1), GetPedTextureVariation(ped, 1)

    if mask == 0 then
        QBCore.Functions.Notify("目标玩家没有面具!", "error")
        return
    end

    QBCore.Functions.Progressbar("maskoff", "拿下面具..", 600, false, true, {
        disableMovement = false,
        disableCarMovement = false,
        disableMouse = false,
        disableCombat = false,
    }, {
        animDict = "missfbi4",
        anim = "takeoff_mask",
        flags = 49,
    }, {}, {}, function() -- Done
        -- 移除目标玩家的面具
        SetPedComponentVariation(ped, 1, 0)

        -- 通过 source 获取执行命令的玩家信息
        local xPlayer = QBCore.Functions.GetPlayer(commandSource)  -- 通过 commandSource 获取玩家信息
        if xPlayer then
            -- 给执行命令的玩家添加面具物品
            TriggerServerEvent("ayu-mask:server:GiveMaskItemA", mask, tex, commandSource)
        else
            QBCore.Functions.Notify("无法获取玩家信息!", "error")
        end
    end, function() -- Cancel

    end)
end)

server.lua

RegisterCommand("unmask", function(source, args)
    local targetId = tonumber(args[1])  -- 获取目标玩家 ID
    if targetId then
        local xPlayer = QBCore.Functions.GetPlayer(targetId)

        if xPlayer then
            -- 触发客户端事件,让目标玩家摘下面具,同时获取执行命令玩家的信息
            TriggerClientEvent("ayu-mask:client:MaskOffA", targetId, source)  -- 传递执行命令的玩家 ID
        else
            TriggerClientEvent('QBCore:Notify', source, "目标玩家无法找到!", "error")
        end
    else
        TriggerClientEvent('QBCore:Notify', source, "请指定有效的玩家 ID!", "error")
    end
end)


RegisterNetEvent('ayu-mask:server:GiveMaskItemA', function(mask, tex, commandSource)
    if not commandSource then
        print("Command source is null!")
        return
    end

    if mask == 0 then return end
    local info = {drawableId = mask, textureId = tex}

    -- 通过 source 获取玩家对象
    local xPlayer = QBCore.Functions.GetPlayer(commandSource)
    if xPlayer then
        -- 给执行命令的玩家添加面具物品
        local success = exports.ox_inventory:AddItem(commandSource, "mask", 1, info)

        if success then
            TriggerClientEvent('ox_lib:notify', commandSource, { type = 'success', description = '你偷走了一个面具!' })
        else
            TriggerClientEvent('ox_lib:notify', commandSource, { type = 'error', description = '偷走面具失败!' })
        end
    else
        TriggerClientEvent('QBCore:Notify', commandSource, "玩家信息获取失败!", "error")
    end
end)
#

brother you @stark bridge and @visual field (please...

#

This is the error I get

stark bridge
#

What is line 88?

stark bridge
craggy oasis
#

local xPlayer = QBCore.Functions.GetPlayer(commandSource) -- 通过 commandSource 获取玩家信息

#

this one line 88

#

But I don't know how to make unmask detect the player in front... Please teach me again

stark bridge
craggy oasis
#

What does this mean? Message not sent from server

stark bridge
#

So commandSource isnt getting set so it is nil.

craggy oasis
#

client.lua

RegisterNetEvent('ayu-mask:client:MaskOffA', function(targetId, commandSource)
    print("收到事件: MaskOffA, 目標玩家 ID: " .. tostring(targetId) .. ", 指令執行者 ID: " .. tostring(commandSource))

    if not targetId or not commandSource then
        QBCore.Functions.Notify("無效的玩家 ID!", "error")
        return
    end

    -- 取得目標玩家的Ped
    local targetPed = GetPlayerPed(GetPlayerFromServerId(targetId))

    -- 檢查目標玩家是否有佩戴面具
    local maskDrawable = GetPedDrawableVariation(targetPed, 1)
    local maskTexture = GetPedTextureVariation(targetPed, 1)

    if maskDrawable == 0 then
        TriggerServerEvent('QBCore:Notify', commandSource, "目標玩家沒有面具!", "error")
        return
    end

    QBCore.Functions.Progressbar("maskoff", "拿下面具..", 600, false, true, {
        disableMovement = false,
        disableCarMovement = false,
        disableMouse = false,
        disableCombat = false,
    }, {
        animDict = "missfbi4",
        anim = "takeoff_mask",
        flags = 49,
    }, {}, {}, function() -- 完成
        -- 移除面具
        SetPedComponentVariation(targetPed, 1, 0)

        -- 觸發伺服器端事件,把面具給執行命令的玩家
        TriggerServerEvent("ayu-mask:server:GiveMaskItemA", maskDrawable, maskTexture, commandSource)
    end, function() -- 取消
        -- 可以選擇在這裡處理取消時的行為
    end)
end)


RegisterCommand("unmask", function()
    local closestPlayer, closestDistance = GetClosestPlayer()

    if closestPlayer and closestDistance < 1.5 then  -- 限制範圍在 3 公尺內
        local targetId = GetPlayerServerId(closestPlayer) -- 取得最近玩家的伺服器 ID
        TriggerServerEvent("ayu-mask:server:TryUnmask", targetId)
    else
        QBCore.Functions.Notify("附近沒有玩家!", "error")
    end
end, false)

-- 取得最近的玩家
function GetClosestPlayer()
    local players = GetActivePlayers()
    local closestPlayer = nil
    local closestDistance = -1
    local plyPed = PlayerPedId()
    local plyCoords = GetEntityCoords(plyPed)

    for _, player in ipairs(players) do
        local targetPed = GetPlayerPed(player)
        if targetPed ~= plyPed then -- 排除自己
            local targetCoords = GetEntityCoords(targetPed)
            local distance = #(plyCoords - targetCoords) -- 計算距離

            if closestDistance == -1 or distance < closestDistance then
                closestPlayer = player
                closestDistance = distance
            end
        end
    end

    return closestPlayer, closestDistance
end

server.lua


RegisterNetEvent("ayu-mask:server:TryUnmask", function(targetId)
    local source = source  -- 執行指令的玩家 ID

    if not targetId then
        TriggerClientEvent('QBCore:Notify', source, "沒有找到附近的玩家!", "error")
        return
    end
    local xPlayer = QBCore.Functions.GetPlayer(targetId)
    if not xPlayer then
        --TriggerClientEvent('QBCore:Notify', source, "目標玩家無法找到!", "error")
        return
    end
    --print("📢 執行命令玩家 ID: " .. tostring(source) .. ", 目標玩家 ID: " .. tostring(targetId))

    TriggerClientEvent("ayu-mask:client:MaskOffA", targetId, targetId, source)
end)




RegisterNetEvent('ayu-mask:server:GiveMaskItemA', function(mask, tex, receiverId)
    if mask == 0 then return end
    local src = receiverId  -- 把物品給執行 `/unmask` 指令的玩家
    local info = {drawableId = mask, textureId = tex}

    local success = exports.ox_inventory:AddItem(src, "mask", 1, info)

    if success then
        TriggerClientEvent('ox_lib:notify', src, { type = 'success', description = '你獲得了一個面具!' })
    else
        --TriggerClientEvent('ox_lib:notify', src, { type = 'error', description = '新增面具失敗!' })
    end
end)


RegisterNetEvent('QBCore:Notify', function(source, message, messageType)
    -- 根據需要處理通知的邏輯
    TriggerClientEvent('QBCore:Notify', source, message, messageType)
end)
#

Now all functions are working properly, but how can I display the progress bar of taking off the mask on the commandSource instead of the target player?

stark bridge
#

TriggerClientEvent("ayu-mask:client:MaskOffA", source, targetId, source)

craggy oasis
#

i fixed

#

@stark bridge one question

stark bridge
craggy oasis
#

wait me

#

hha

#

sorry

#

update

#

Do you know which action the police officer takes off his mask in in the video?

#

animDict = "misscarsteal4@actor",
anim = "actor_berating_loop",

stark bridge
stark bridge
craggy oasis
#

Yes
This is my current action
But I want to change it to the animation of the police officer taking off his mask. I don't know which one...

craggy oasis
#

NO🤔

stark bridge
craggy oasis
#

I'm talking about this "raising hands" animation of the police officer, which is similar to a debate or taking off a mask.
Which anime is he from, I can't find it