I made a script that lets the player move the mouse freely when they pressed a specific key bind but I don’t know how to lock the mouse back.
I tried UserInputService.MouseBehavior but I don’t know how to use it, I need help
I made a script that lets the player move the mouse freely when they pressed a specific key bind but I don’t know how to lock the mouse back.
I tried UserInputService.MouseBehavior but I don’t know how to use it, I need help
I believe using Enum.MouseBehavior.LockCenter
is what you have to use to change hte mousebehavior to center locking
Thank you, but I tried and it still doesn’t work I don’t know if I’m doing something wrong
Could you send me your code so I can see what you’re doing
I just started scripting, so is kind of difficult for me to identify errors
This is the one I use to let the player move the mouse freely
And the one that is supposed to lock the mouse back
Output
The reason in this case it doesn’t work is because textButton’s modal is set to true. Modal is a property that, when enabled on a textbutton, makes the mouse be able to move freely. Also, it’s much more preferable to change from using the mouse.KeyDown to UserInputService.InputBegan.
Here’s how it should be instead for both keybinds
Code for free movement
local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(input,processed)
if processed then return end
if input.KeyCode == Enum.KeyCode.E then
uis.MouseBehavior = Enum.MouseBehavior.Default --Free movement
elseif input.KeyCode == Enum.KeyCode.R then
uis.MouseBehavior = Enum.MouseBehavior.LockCenter --Center mouse
end
end)
This hopefully will work better than using a textbutton’s modal to free the mouse
Edit: If that doesn’t work, then what you do instead is something like this
local uis = game:GetService("UserInputService")
local gui = game.Players.LocalPlayer:WaitForChild("PlayerGui")
local scrgui = Instance.new("ScreenGui", gui)
local button = Instance.new("TextButton")
button.BackgroundTransparency = 1
button.Size = UDim2.New(0,0,0,0)
button.Modal = false
button.Parent = scrgui
uis.InputBegan:Connect(function(input,processed)
if processed then return end
if input.KeyCode == Enum.KeyCode.E then
button.Modal = true
elseif input.KeyCode == Enum.KeyCode.R then
button.Modal = false
end
end)
I forgot to make my reply a reply towards what you said, sorry about that. I think it’s much better you test out the code after the edit instead as it should have a higher chance of working. So just replace the code you currently have with that and if that works
I am pretty sure UserInputService.MouseBehavior resets constantly to the default one.
I usually use RunService.RenderStepped to constantly set the Mouse to the center, and when I want it to stop I just :Disconnect() the connection.
Since RunService.RenderStepped became Deprecated, I believe one of these four events is the replacement:
I am pretty sure PreRender is the replacement of RenderStpped, but If doesn’t work, try RenderStepped
For instance:
local UIS = game:GetService("UserInputService")
local conn -- We will save teh connection here.
UIS.InputBegan:Connect(function(input, process)
if not process then --If the player isn't chatting
if(input.KeyCode == Enum.KeyCode.R) then
conn = game:GetService("RunService").PreRender:Connect(function(deltaTime) --Constantly set the mouse to LockCenter
UIS.MouseBehavior = Enum.MouseBehavior.LockCenter --Set the mouse to Conter
end
elseif(input.KeyCode == Enum.KeyCode.E) then
if conn then conn:Disconnect() end --Step setting (will automaticly go to default)
end
end
end
What is the code you currently have? I’m unsure if I’ll be ableto respond since I may be gone in a few minutes
I have this one
local uis = game:GetService("UserInputService")
local gui = game.Players.LocalPlayer:WaitForChild("PlayerGui")
local scrgui = Instance.new("ScreenGui", gui)
local button = Instance.new("TextButton")
button.BackgroundTransparency = 1
button.Size = UDim2.New(0,0,0,0)
button.Modal = false
button.Parent = scrgui
uis.InputBegan:Connect(function(input,processed)
if processed then return end
if input.KeyCode == Enum.KeyCode.E then
button.Modal = true
elseif input.KeyCode == Enum.KeyCode.R then
button.Modal = false
end
end)
I think I may’ve accidentalyl capitalized the new in UDim2.New
, Lua is case sensitive so try turning UDim2.New
to UDim2.new