example
example

已解决√ 求助 脚本中Lua与JS交互部分求解(Lua无报错)

Prettysavage

新用户
高级用户
认证用户
黄金
0.00G
JavaScript:
window.addEventListener('message', function () {
 if (event.data.type === 'Show') {
     let server = document.querySelector('.server')
     let servername = document.querySelector('.server-name')
     let serverversion = document.querySelector('.server-version')
     let ServerNumber = document.querySelector('.server-number')
     let ServerTime = document.querySelector('.server-time')
     ServerNumber.innerHTML = `- ${event.data.GetPlayerNumber}/500`
     ServerTime.innerHTML = `- ${event.data.GetServeTime}`
     server.style.display = 'block'
     servername.style.display = 'block'
     serverversion.style.display = 'block'
     ServerNumber.style.display = 'block'
     ServerTime.style.display = 'block'
 }
})

Lua:
--客户端部分
local QBCore = exports['qb-core']:GetCoreObject()

RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
    QBCore.Functions.TriggerCallback('GetServerDetail', function(data)
      SendNuiMessage(data)
      print('client')
    end)
end)

--服务器端部分
local QBCore = exports['qb-core']:GetCoreObject()

QBCore.Functions.CreateCallback('GetServerDetail', function(source, cb)
  print('serve1')
  local data = {
    type = 'Show',
    GetPlayerNumber = GetNumPlayerIndices(),
    GetServeTime = '[' .. os.date('%X') .. '] '
  }
  cb(data)
  print('server2')
end)
 
最后编辑:
JS部分我DOM了一下,但是在游戏里发现没显示出来,Lua部分断点测试无误.
 
print打印出来东西了嘛, 我从你的代码里边看好像压根不会执行
 
你可以加群把脚本发我,这些东西看起来没有毛病,我可以帮你测试然后改一下。
 
或者直接传到论坛
 
你原代码当中没有写形参
JavaScript:
window.addEventListener('message', function () {
修改后:
JavaScript:
window.addEventListener('message', function (event) {
 
今天游戏有问题,我进不去游戏测试,你修改一下看行不行。
 
JavaScript:
window.addEventListener('message', function () {
 if (event.data.type === 'Show') {
     let server = document.querySelector('.server')
     let servername = document.querySelector('.server-name')
     let serverversion = document.querySelector('.server-version')
     let ServerNumber = document.querySelector('.server-number')
     let ServerTime = document.querySelector('.server-time')
     ServerNumber.innerHTML = `- ${event.data.GetPlayerNumber}/500`
     ServerTime.innerHTML = `- ${event.data.GetServeTime}`
     server.style.display = 'block'
     servername.style.display = 'block'
     serverversion.style.display = 'block'
     ServerNumber.style.display = 'block'
     ServerTime.style.display = 'block'
 }
})

Lua:
--客户端部分
local QBCore = exports['qb-core']:GetCoreObject()

RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
    QBCore.Functions.TriggerCallback('GetServerDetail', function(data)
      SendNuiMessage(data)
      print('client')
    end)
end)

--服务器端部分
local QBCore = exports['qb-core']:GetCoreObject()

QBCore.Functions.CreateCallback('GetServerDetail', function(source, cb)
  print('serve1')
  local data = {
    type = 'Show',
    GetPlayerNumber = GetNumPlayerIndices(),
    GetServeTime = '[' .. os.date('%X') .. '] '
  }
  cb(data)
  print('server2')
end)
服务端代码需要修正:
Lua:
cb(json.encode(data))
需要把lua的table转换成JSON格式就可以了
 
后退
顶部