Trying to detect left trigger while looking at a block

Hi, I’m trying to make a puzzle-solving game where if you right click while looking at a block, the block extends, and if you left click, the block retracts. I’m using a ClickDetector for detecting left click and right click. I’m able to detect right trigger on a controller, but left trigger does nothing. I’m trying to get left trigger to act as right click. I have tried using the code below, but it doesn’t want to work, and even if it did, it would activate even when I’m not looking directly at the block.

game:GetService("UserInputService").InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.ButtonL2 then
		main()
	end
end)

The script is a normal script in the block, and I’d preferably like to keep it that way so I can have multiple instances of the block around the game without having to specifically call each one locally.
Any help is greatly appreciated. Thank you!

Edit: I did forget to mention I have looked everywhere on the dev forum and I haven’t found anything related to the problem I’m having.

1 Like

Well when I try to detect the button at all, nothing happens. I have tried this basic code in a script in the block which should detect if the player hits the X button on the controller, but nothing happens.

game:GetService("UserInputService").InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.ButtonX then
		print("Player pressed X")
	end
end)

Maybe you can’t detect gamepad input in a server script?

2 Likes

User inputs as a whole don’t work in server scripts and have to be handled in a local script.

3 Likes

a simple raycast check would fix the problem:

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")


local block = workspace:WaitForChild("Block")
local plr = Players.LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
local mouse = plr:GetMouse()

local rayParams = RaycastParams.new()

rayParams.FilterType = Enum.RaycastFilterType.Whitelist
rayParams.FilterDescendantsInstances = {block}

UserInputService.InputBegan:Connect(function(i, isTyping)
	if isTyping then return end
	
	if i.UserInputType == Enum.UserInputType.MouseButton1 then
		local raycast = workspace:Raycast(character.Head.Position, (mouse.Hit.Position - character.Head.Position).Unit * 1000, rayParams)
		
		if raycast then
			print(raycast.Instance)
			-- Do stuff
		end
	end
end)

This will send a ray from the player’s head to the position of the mouse.

1 Like

you would listen for the left/right trigger via userinputservice, then you’d check if the players mouse.Target is a puzzle block, if so you’d send a request to the server via remoteevents to either extend or retract it. (keep in mind you have to do the checks on a local script)

1 Like

Thanks all for the help, I figured out how to do it by calling the ClickDetector from a LocalScript in StarterPlayerScripts and a RemoteEvent in the block.

local debounce = false
local incd = false

local val = 0

function db()
	debounce = true
	wait(0.4)
	debounce = false
end

workspace.Red1.ClickDetector.MouseHoverLeave:Connect(function()
	incd = false
end)

workspace.Red1.ClickDetector.MouseHoverEnter:Connect(function()
	incd = true
	game:GetService("UserInputService").InputEnded:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.ButtonL2 then
			if incd == true then
				if debounce == false then
					print("PRESSED: ".. val)
					debounce = true
					workspace.Red1.ControllerDown:FireServer()
					wait(0.4)
					debounce = false
				end
			end
		end
	end)
end)

workspace.Red1.ClickDetector.MouseClick:Connect(db)
workspace.Red1.ClickDetector.RightMouseClick:Connect(db)
1 Like