Help with code not working

  1. What do you want to achieve? The mouse to be unlocked and visible when key pressed

  2. *What is the issue? mouse behavior won’t change

  3. What solutions have you tried so far? disabling and re-enabling the mouse using the script by using game:GetService("UserInputService").MouseIconEnabled

showing the ui and hiding it works perfectly fine.

local plr = game.Players.LocalPlayer
local ui = script.Parent

plr:GetMouse() .KeyDown:Connect(function(Keycode)
	if Keycode == "u" then
	ui.Visible = true
	  game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default
		game:GetService("UserInputService").MouseIconEnabled = true
	else
		ui.Visible = false
		game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.LockCenter
		game:GetService("UserInputService").MouseIconEnabled = false
	end
end)

Extras

Screenshot 2024-04-21 195450

(All the code is in “UiHandler”)

1 Like

You are using the mouse to check for keyboard input. :grin:
Try this code:

local plr = game.Players.LocalPlayer
local ui = script.Parent
local uis = game:GetService("UserInputService")

uis.InputEnded:Connect(function(inputObject,gameProcessedEvent)
	if gameProcessedEvent then
		--This means if the player is chatting or something like that
		return
	end

	if inputObject.KeyCode == Enum.Keycode.U then
		ui.Visible = true
		game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default
		game:GetService("UserInputService").MouseIconEnabled = true
	else
		ui.Visible = false
		game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.LockCenter
		game:GetService("UserInputService").MouseIconEnabled = false
	end
3 Likes

Thanks but it doesn’t work + some parts are underlined red/orange (which obviously means error!!!) and have no clue on how to fix it

1 Like

add end) at the end

you have to close the uis event

1 Like

Already tried this:
image
I done it again so I can send the screenshot

1 Like
local plr = game:GetService("Players")
local ui = script.Parent
local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(input, gpc)
	if gpc then return end
	
	if input.KeyCode == Enum.KeyCode.U then
		ui.Visible = true
		uis.MouseBehavior = Enum.MouseBehavior.Default
		uis.MouseIconEnabled = true
	else
		ui.Visible = false
		uis.MouseBehavior = Enum.MouseBehavior.LockCenter
		uis.MouseIconEnabled = false
	end
end)

This should work.

1 Like

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