UserInputService InputBegan isn't working

I have a LocalScript in StarterCharacterScripts that’s supposed to change the character’s state when clicked, but for some reason when I click, nothing happens at all & there are no errors. I’ve searched for like 2 hours and can’t find anything about this, thank you.

Code:

--// Variables

local Player = game.Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Root = Character:WaitForChild("HumanoidRootPart")
local uis = game:GetService("UserInputService")
local physics = false

Humanoid.HipHeight = -0.5

--// Event

uis.InputBegan:Connect(function(input, gameProcessedEvent)
	
	if gameProcessedEvent then return end
	
	if input == Enum.UserInputType.MouseButton1 then
		
		if physics == false then
			physics = true
			Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
		else
			physics = false
			Humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
		end
	end
	
end)
1 Like

Your script contained:
if input == Enum.UserInputType.MouseButton1 then

The change was:
if input.UserInputType == Enum.UserInputType.MouseButton1 then

The key thing to remember for any use of UserInputService’s InputObject is to check the UserInputType because that will verify if your type of input matches, such as a keyboard or mouse.

There’s a lot more to digest about UserInputService, but you should be okay with just that.

--// Variables

local Player = game.Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Root = Character:WaitForChild("HumanoidRootPart")
local uis = game:GetService("UserInputService")
local physics = false

Humanoid.HipHeight = -0.5

--// Event

uis.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then return end
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if physics == false then
			physics = true
			Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
		else
			physics = false
			Humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
		end
	end
end)
3 Likes

Try this instead. It actually checks if you’re typing in chat or in any other text box.

if UIS:GetFocusedTextBox() then return end

Also if you’re trying to use the mouse, you can just use player:GetMouse() and use the Button1Down event.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Down:Connect(print)

Also also, unless if your local script runs every time the player respawns, this cannot run if the player dies, so just check that.

2 Likes

I’m not sure how or why this works but it did, I read over yours and mine and the only difference I could notice was that an empty line was removed, I’m soo confused but thanks!

1 Like

This also helped me understand a bit more about the player:GetMouse() system. Thanks.

1 Like

My bad, I was in a hurry when I wrote my solution. I can explain a bit more about what changed.

Your script contained:
if input == Enum.UserInputType.MouseButton1 then

The change was:
if input.UserInputType == Enum.UserInputType.MouseButton1 then

The key thing to remember for any use of UserInputService’s InputObject is to check the UserInputType because that will verify if your type of input matches, such as a keyboard or mouse.

There’s a lot more to digest about UserInputService, but you should be okay with just that.

1 Like

Oh thanks! That helped a lot!!!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.