Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Mouse keeps getting unlocked from center? (ANSWERED)

I wanted to make a script so that when I hold LeftAlt it unlocks the mouse from the center, and when you let go, it locks again.

local UserInt = game:GetService("UserInputService")

UserInt.InputBegan:Connect(function(input, gameProcessed, IsTyping)
	
	if IsTyping then return end
	if input.KeyCode == Enum.KeyCode.LeftAlt then
UserInt.MouseBehavior = Enum.MouseBehavior.Default
	end
end)

UserInt.InputEnded:Connect(function(input, gameProcessed, IsTyping)
	
	if IsTyping then return end
	if input.KeyCode == Enum.KeyCode.LeftAlt then
		UserInt.MouseBehavior = Enum.MouseBehavior.LockCenter
	end
end)

but everytime I let go of alt, it goes to the center but its not locked there. (by it being "unlocked" I mean I can still move my mouse across the screen)

https://medal.tv/clips/31967330/d1337sPhVH2W as you can see you can still move it after it goes to the center

Help would be greatly appreciated.

1 answer

I found a way to do this. All you have to do is copy the script, paste it and then delete the previous version so in that way, it seems like you never pressed Alt before.

local UserInt = game:GetService("UserInputService")

UserInt.InputBegan:Connect(function(input, gameProcessed, IsTyping)
	
	if IsTyping then return end
	if input.KeyCode == Enum.KeyCode.LeftAlt then
		local copy = script:Clone()
		copy.Parent = script.Parent
		script:Destroy()
	end
	end)

UserInt.InputEnded:Connect(function(input, gameProcessed, IsTyping)
	
	if IsTyping then return end
	if input.KeyCode == Enum.KeyCode.LeftAlt then
		game:GetService("RunService").RenderStepped:Connect(function()
		UserInt.MouseBehavior = Enum.MouseBehavior.LockCenter
		end)
	end
	end)

This discussion is preserved as it was. New answers, comments, and votes are closed.