How to re-enable mouse icon when GUI appears

Hello,

So I got a TextLabel letter “o” in the center of the screen serving as a cursor for my horror game. I tried replacing the actual game cursor a bit ago with a decal but couldn’t find the exact one I was looking for which once again is why I went with a TextLabel instead. Alright anyways, I got a LocalScript in StarterGui which removes the mouse icon. Looks like this:

local userInputService = game:GetService("UserInputService")

userInputService.MouseIconEnabled = false

Simple stuff, no problem until you come across this one puzzle in my game where a GUI pops up and you manually need to put in the code in clicking the button numbers.


My game is in LockFirstPerson and when the GUI pops up, I have it so the mouse unlocks. The thing is, due to the script from earlier, you almost have to guess where the mouse is at while moving it around. This is not efficient at all and is simply annoying. Now here is the code for the door.

--Waiting for objects to load
local Configuration = script.Parent:WaitForChild("Configuration")
local Code = Configuration:WaitForChild("Code")
local TimeOpen = Configuration:WaitForChild("TimeOpen")
local SlidingTime = Configuration:WaitForChild("SlidingTime")
local Height = Configuration:WaitForChild("Height")
local Button = script.Parent:WaitForChild("Button"):WaitForChild("ProximityPrompt")
local Light = script.Parent:WaitForChild("Light")
local Door = script.Parent:WaitForChild("Door")
--Tweening
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(SlidingTime.Value,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)
--Other variables
local open = false

local opensound = script.Parent.Door.cellopen




Button.Triggered:Connect(function(player)
	local newcode = game.ReplicatedStorage:WaitForChild("CodeDoor"):InvokeClient(player)  --we invoke the players client and wait until they return the code

	if newcode == Code.Value and open == false then
		print('correct')
		open = true
		local goal1 = {CFrame = Door.CFrame + Vector3.new(0,Height.Value,0)}	--CFrame of the door when it moves up, used for tween1
		local goal2 = {CFrame = Door.CFrame}						--Devault CFrame for the door, used for tween2
		local tween1 = tweenService:Create(Door,tweenInfo,goal1)	--opening tween
		local tween2 = tweenService:Create(Door,tweenInfo,goal2)	--closing tween
		Light.Color = Color3.fromRGB(73, 181, 71)					--Light (part) color changes
		Light.PointLight.Color = Color3.fromRGB(73, 181, 71)--PointLight color changes (optional, disabled by default)
		tween1:Play()												--The door opens because we play the opening tween
		opensound:Play()
		script.Parent.Button.ProximityPrompt.Enabled = false
		script:Destroy()
	else 		--If the code is wrong, print "incorrect"
		print('incorrect') 
	end
end)

I want to know if there’s a way to show the mouse again when the GUI is up and disappear again once closed with how things are set up right now. I’ve tried putting

userInputService.MouseIconEnabled = true

Below the

if newcode == Code.Value and open == false then

line from the top of the door’s script, when then I then put

userInputService.MouseIconEnabled = false

Right below

script.Parent.Button.ProximityPrompt.Enabled = false

at the bottom of the door’s script in hopes that it would somehow work. Unfortunately it didn’t and since I don’t really specialize in scripting I wouldn’t know what to do. I really hope this makes sense and I’d truly appreciate any help!

1 Like

Have you tried changing the MouseIcon itself to see if it reappears?

Yeah I believe so, I even said about it here.

Not so sure how to fix the issue in the clip.

Hey hey!

So, in ImageButtons and TextButtons, there’s a boolean value called “Modal.” When that is set to true, your mouse will unlock whenever that button is visible.

You don’t really need to script anything, just set one of your GuiButtons.Modal to true in the properties tab and it should work with the rest of your code automatically.

1 Like

Greetings,

Yeah I already have it set up like that. My concern is the script that I have for removing the default mouse icon.

userInputService.MouseIconEnabled = false

The round “mouse icon” that you see in the clip is actually a ScreenGui TextLabel, so in other words it’s a fake icon in-game. When the GUI pops up though, I’d like the default one to be visible again so you can see what you’re clicking on. I was wondering what can be implemented in the door’s script so the default icon re-appears when the GUI is up, taking the fact that there’s a script in game for what would usually have the normal icon disabled. I really hope this makes sense haha, tried my best explaining as I’m bad at that too.

No, you explained it just fine. I understand perfectly.

Instead of making the default mouse visible, why don’t you run a RenderedStepped function that makes the fake mouse position the position of the player’s mouse?

There’s a function that belongs to the camera (workspace.CurrentCamera) called “WorldToScreenPoint.” If you give it the player’s mouse’s hit position, we can get the Vector2 position and then use those variables inside the Vector2 to allow the TextLabel to follow the mouse

local Camera = workspace.CurrentCamera -- Get the camera so we can use the "WorldToScreenPoint" function

local FakeMouse = path.To.TextLabel -- Variable for the "o" TextLabel or in other words fake mouse
local RealMouse = game.Players.LocalPlayer:GetMouse() -- Get the player's real mouse so we can find it's hit position.

game:GetService("RunService").RenderStepped:Connect(function() -- Run the RenderedStepped function, which fires every frame prior to it being rendered, allowing us to basically loop the function without halting the rest of the script.
	local WorldToScreenPoint = Camera:WorldToScreenPoint(RealMouse.Hit.Position) -- Turn the Vector3 (mouse) position to a Vector2 position
	
	FakeMouse.Position = UDim2.new(0, WorldToScreenPoint.X, 0, WorldToScreenPoint.Y) -- Take the X and Y coordinates from the Vector2 and put them in the offsets off the FakeMouse's position.
end)
1 Like

Ohh alrighty, where would you recommend the code you provided to go? In the door’s script or make a new script under the TextLabel with the code there? If it were to be under the TextLabel then like how would it know when the GUI pops up to then start following the mouse position? When the GUI disappears how would the TextLabel know when to return back to the center? Sorry I’m just a bit confused as I’m alright at understanding scripting and like where things should go.

1 Like

Don’t apologize for being confused!

Anywhere is fine, preferably in the same ScreenGui that the code pin is in for organization purposes.

Since you have “Modal” set to true on one of your buttons, the script doesn’t need to know when the mouse is unlocked. Since the mouse automatically locks/unlocks when that button is visible/invisible, the “o” will just follow wherever the mouse is, meaning that when the mouse locks again, the “o” will go back to the center of the screen.

Here’s an example, without any further modification of the script, the mouse will automatically lock when I’m in third person.

I know you’re not going to be in third person at all in this game, but the same rule still applies! So, you don’t have to tell the script when the mouse is unlocked.

1 Like

Makes a bit more sense now, there’s one last thing though.

In line 3 you can see some sort of error.


This is what is shown in the output. Not so sure what happened there.

Since I don’t know where your TextLabel is in the Explorer, I put “path.to.textlabel.” You’ll have to write the path to where the “o” TextLabel is.

1 Like

I’m so blind, fixed. I tested things now but for some reason it still doesn’t want to work. There’s no errors in the output either relating to the code.

Clip:

What do you think may be causing this to keep happening even after everything seems fine?

That is very odd. It works just fine for me when I tried it.

Here’s an open-source project in case you wanna dive in and see if you can find any difference between yours and mine:
UnlockingMouse.rbxl (37.5 KB)

I’ll keep investigating on my end.

1 Like

Adding on…if you want…I know you said that you couldn’t find the right icon, but would this be something similar to what you were looking for to begin with?

I think if all of these methods fail you should just use an image as the cursor’s icon.

1 Like

Yeah I tried with that first yesterday but the decal I uploaded for the cursor wouldn’t change. The default icon was still there, oh well. That would be a whole other topic.

Edit: By the way, I really appreciate you trying to help me out as no one was doing that.

1 Like

Of course! Sorry you haven’t been able to solve this yet!


We’ll only do this if you’re comfortable with it. But if you want, would it be possible to DM me your Guis you’re using for this lock pin so I can maybe investigate what’s going on? Again, only if you’re comfortable with it.

1 Like