Roblox:ServerScriptService.DamagableManager:48:尝试索引本地'mouse'(nil值)

问题描述 投票:0回答:1

我正在尝试获取玩家的鼠标位置。但是我正在获取ServerScriptService.DamagableManager:48:尝试索引本地“鼠标”(nil值)错误。

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

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()

     --Unrelated codes here
end

CheckIfDamagable.OnServerInvoke = CheckIfDamagableObject
lua roblox
1个回答
1
投票

如您所发现,将鼠标对象传递给服务器时,它变为零。根据Player:GetMouse()上的文档:

此项目必须在LocalScript中使用,才能按预期方式在线工作。

您应该只将鼠标放在客户端上的位置,然后使用RemoteFunction将结果传递给服务器。

LocalScript

-- get the mouse object
local mouse = game.Players.LocalPlayer:GetMouse()

local function onMousedClicked(actionName,inputState, inputObject)
    if inputState == Enum.UserInputState.Begin then
        -- pass the mouse position and any other information to the server
        local result = CheckIfDamagable:InvokeServer(mouse.Hit)
        print("Mouse Clicked : ", result)
    end
end

脚本

function CheckIfDamagableObject(player, mousePos)
    print("Mouse CFrame : ", mousePos)

    -- Unrelated codes here
end

CheckIfDamagable.OnServerInvoke = CheckIfDamagableObject
© www.soinside.com 2019 - 2024. All rights reserved.