【问题标题】:How would I make this script stop sending the mouse's hit coordinates to the server after the player hits a specific key?在玩家点击特定键后,如何让这个脚本停止将鼠标的点击坐标发送到服务器?
【发布时间】:2020-02-08 08:27:48
【问题描述】:

所以,我目前在玩家屏幕上有一个按钮,当他们点击它时,它会检查玩家是否点击了世界上的任何地方,并将鼠标的点击坐标发送到服务器。当玩家点击鼠标左键时,服务器将使用这些来制作特定部件(它将很快与模型一起使用)移动到鼠标所在的任何位置。那工作得很好。但是,我想做到如果玩家在设置零件位置之前或之后点击字母“E”,它将基本上停止向服务器发送鼠标点击数据,然后继续前进。

我还在学习如何使用 UserInputService。 UserInputService.InputBeganUserInputService.InputEnded 并没有像我希望的那样工作。有人可以帮我解决这个问题吗?

代码如下:

本地脚本:

userInputService.InputBegan:Connect(function(input)     
        if input.UserInputType == Enum.UserInputType.MouseButton1 then
            movePartEvent:FireServer(math.ceil(mouse.Hit.X), math.ceil(mouse.Hit.Y), math.ceil(mouse.Hit.Z))
        end

        if input.UserInputType == Enum.KeyCode.E then
            print("Part placed...")
        --Exit the userInput part of the code here --
        end
    end)

服务器脚本:

local replicatedStorage = game:GetService("ReplicatedStorage")
local movePartEvent = replicatedStorage:WaitForChild("MovePartEvent")

movePartEvent.OnServerEvent:Connect(function(...)   
    local tuppleArgs = {...}
    local player  = tuppleArgs[1]
    local value1  = tuppleArgs[2]
    local value2  = tuppleArgs[3]
    local value3  = tuppleArgs[4]

    local function movePartOnEvent(part)        
        part.Position = Vector3.new(value1, value2, value3)
    end

    movePartOnEvent(game.Workspace.MovingPart)
end)

提前致谢!

【问题讨论】:

  • 所以我决定改用 Mouse.KeyDown 函数。我刚刚了解到您仍然可以使用它。但是,我不介意仍然得到上面代码的答案,因为它将来可能会派上用场。谢谢。

标签: lua user-input roblox


【解决方案1】:

我会添加一个变量来决定客户端是否仍在向服务器发送数据。当你按“E”时,它会改变变量的值。

local movingPart = true;

userInputService.InputBegan:Connect(function(input)     
        if input.UserInputType == Enum.UserInputType.MouseButton1 and movingPart then
            movePartEvent:FireServer(math.ceil(mouse.Hit.X), math.ceil(mouse.Hit.Y), math.ceil(mouse.Hit.Z))
        end

        if input.UserInputType == Enum.KeyCode.E then
            print("Part placed...")
            movingPart = false;

        --Exit the userInput part of the code here --
        end
    end)

现在,当您按下“E”时,它会停止向服务器发送鼠标位置。如果要移动另一部分,则必须再次将变量“movingPart”设置为 true。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    • 2012-05-31
    相关资源
    最近更新 更多