Roblox:ServerScriptService.DamagableManager:48:尝试索引本地“mouse”(空值)

我试图获取玩家的鼠标位置。但是我得到了 ServerScriptService.DamagableManager:48: 尝试索引本地 'mouse'(空值)错误。

我正在尝试连接到服务器脚本的本地脚本:

local function onMousedClicked(actionName,inputState,inputObject)
    if inputState == Enum.UserInputState.Begin then
        CheckIfDamagable:InvokeServer()
        print"MouseClicked")
    end
end

我正在尝试获取鼠标信息的服务器脚本:

function CheckIfDamagableObject(player)
     local mouse = player:GetMouse()

     --不相关的代码在此处
end

CheckIfDamagable.OnServerInvoke = CheckIfDamagableObject
点赞
用户2860267
用户2860267

如你所发现的那样,将鼠标对象传递给服务器后,它变成了 nil。根据 Player:GetMouse() 的文档:

此项必须在 LocalScript 中使用才能在在线情况下达到预期效果。

你应该在客户端获取鼠标位置,并将结果通过你的 RemoteFunction 传递给服务器。

LocalScript

-- 获取鼠标对象
local mouse = game.Players.LocalPlayer:GetMouse()

local function onMousedClicked(actionName, inputState, inputObject)
    if inputState == Enum.UserInputState.Begin then
        -- 将鼠标位置和任何其他信息传递给服务器
        local result = CheckIfDamagable:InvokeServer(mouse.Hit)
        print("鼠标点击:", result)
    end
end

Script

function CheckIfDamagableObject(player, mousePos)
    print("鼠标 CFrame:", mousePos)

    -- 非相关代码在此
end

CheckIfDamagable.OnServerInvoke = CheckIfDamagableObject
2020-03-06 01:38:24