[SOLVED] I tried making a 'UserInputService' script, but something is kinda wrong

So, When i press my mouse, the script run, i dont know why. Here is the script:

-->variables


local user_input_service = game:GetService('UserInputService')
local cellphone = script.Parent:WaitForChild('cellphone')


-->script


user_input_service.InputBegan:connect(function(input)
	
	
	if input.KeyCode == Enum.KeyCode.Tab and cellphone.Position == UDim2.new(0.189, 0,1.202, 0) then
		
		
		cellphone.Position = UDim2.new(0.189, 0,0.74, 0)
		
		
		else
		
		
		cellphone.Position = UDim2.new(0.189, 0,1.202, 0)
		
		
	end
	
	
end)

Thanks for Reading, TinkyWinkyDev.

What seems to be the problem? any errors?

You have an else statement, basically saying if it wasn’t the tab key being clicked, it will go to the else statement.

local user_input_service = game:GetService('UserInputService')

user_input_service.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.Tab then
		-- When tab is pressed
		print("A")
	else
		-- When any other key is pressed (including mouse)
		print("B")
	end
end)

Try removing the else, that should work.

If you are trying to check if the cellphones position changed, use elseif instead.

local user_input_service = game:GetService('UserInputService')

user_input_service.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.Tab then
		-- When tab is pressed
		print("A")
	elseif true == true then -- Check if the cellphones position is different
		-- When any other key is pressed (including mouse)
		print("B")
	end
end)

Your script should look like this if you are using the elseif statement.

local user_input_service = game:GetService('UserInputService')
local cellphone = script.Parent:WaitForChild('cellphone')

user_input_service.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.Tab and cellphone.Position == UDim2.new(0.189, 0,1.202, 0) then
		cellphone.Position = UDim2.new(0.189, 0,0.74, 0)
	elseif cellphone.Position == UDim2.new(0.189, 0,1.202, 0) then
		cellphone.Position = UDim2.new(0.189, 0,1.202, 0)
	end
end)
2 Likes

Thanks for the help, you helped a lot.

You are very welcome, I’m glad it helped.