如何让这个脚本在玩家点击特定键后停止向服务器发送鼠标点击坐标?

目前,我在玩家屏幕上放置了一个按钮,当他们点击时,它将检查玩家在世界中任何地方点击鼠标,然后将它们的鼠标点击坐标发送到服务器上。服务器将使用这些坐标将特定的部件(它很快将与模型一起工作)移动到播放器单击左鼠标按钮时的位置。目前一切工作正常。但是,我想要的是,如果玩家在设置部件位置之前或之后按字母'E',它就会停止向服务器发送鼠标点击数据并继续执行。

我仍在学习如何使用UserInputService。_UserInputService.InputBegan_和_UserInputService.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("部件已放置...")
        --在这里退出userInput代码--
        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)

提前感谢!

点赞
用户9765531
用户9765531

我会添加一个变量,决定客户端是否仍在向服务器发送数据。当您按下“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("部件已放置...")
            movingPart = false;

        -- 在此退出userInput部分的代码--
        end
    end)

现在,当您按下“E”键时,它会停止向服务器发送鼠标位置。如果您想移动另一个部分,您需要将变量“movingPart”重新设置为true。

2019-11-16 22:49:22