Robloxでユーザーがどのような入力をしたかを検知する

UserInputServiceを利用することでユーザーの入力を検知することができる

基本的な入力の検知

ジャンプ入力とキー入力を検知する

StarterPlayer -> StarterPlayerScriptsの配下にローカルスクリプトを作成、名前をDetectUserInputに変更する

スクリプトを以下のように編集する

-- UserInputServiceを取得
local UserInputService = game:GetService("UserInputService")
-- ジャンプ入力を検知
UserInputService.JumpRequest:Connect(function()
print("jump")
end)
-- キー入力開始を検知
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.Keyboard then
local keyPressed = input.KeyCode
print("A key is being pushed down! Key:",input.KeyCode)
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
print("The left mouse button has been pressed down at",input.Position)
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
print("The right mouse button has been pressed down at",input.Position)
elseif input.UserInputType == Enum.UserInputType.Touch then
print("A touchscreen input has started at",input.Position)
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
print("A button is being pressed on a gamepad! Button:",input.KeyCode)
end
end)
-- UserInputServiceを取得 local UserInputService = game:GetService("UserInputService") -- ジャンプ入力を検知 UserInputService.JumpRequest:Connect(function() print("jump") end) -- キー入力開始を検知 UserInputService.InputBegan:Connect(function(input, gameProcessedEvent) if input.UserInputType == Enum.UserInputType.Keyboard then local keyPressed = input.KeyCode print("A key is being pushed down! Key:",input.KeyCode) elseif input.UserInputType == Enum.UserInputType.MouseButton1 then print("The left mouse button has been pressed down at",input.Position) elseif input.UserInputType == Enum.UserInputType.MouseButton2 then print("The right mouse button has been pressed down at",input.Position) elseif input.UserInputType == Enum.UserInputType.Touch then print("A touchscreen input has started at",input.Position) elseif input.UserInputType == Enum.UserInputType.Gamepad1 then print("A button is being pressed on a gamepad! Button:",input.KeyCode) end end)
-- UserInputServiceを取得
local UserInputService = game:GetService("UserInputService")

-- ジャンプ入力を検知
UserInputService.JumpRequest:Connect(function()
	print("jump")
end)

-- キー入力開始を検知
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		local keyPressed = input.KeyCode
		print("A key is being pushed down! Key:",input.KeyCode)
	elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
		print("The left mouse button has been pressed down at",input.Position)
	elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
		print("The right mouse button has been pressed down at",input.Position)
	elseif input.UserInputType == Enum.UserInputType.Touch then
		print("A touchscreen input has started at",input.Position)
	elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
		print("A button is being pressed on a gamepad! Button:",input.KeyCode)
	end
end)

結果

関連

https://developer.roblox.com/en-us/api-reference/property/InputObject/UserInputType