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

Mouse unlock script for FPS not working?

So im making a FPS game right now and you should be able to switch teams. The problem is its locked in first person and i need you need to unlock the Mouse in order to press the "Change Team" Button. I thought about making it modal it works but it doesn't really work mit my fps since you need to hold right click to look around in first person and i dont want that. I want to lets say Press M to unlock the Mouse and then press M again to lock it

Im pretty new to scripting so i made this:

local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")
local debounce = false

wait(1)
game:GetService("UserInputService").InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.M and debounce == false then
		debounce = true
		uis.MouseBehavior = Enum.MouseBehavior.LockCenter
		
		
	elseif input.KeyCode == Enum.KeyCode.M and debounce == true then
		debounce = false
		uis.MouseBehavior = Enum.MouseBehavior.Default
	end	
end)

But that doesnt work and i dont know why

4 comments

0

What exactly do you mean by "doesnt work"

0

Well its not working and i dont know what i did wrong

0

I belive you should use context action service, as User Input Service is for server, context action service is for client. https://developer.roblox.com/en-us/api-reference/class/ContextActionService

0

@marine5575 wrong. UIS or CAS can be used in both situations. It's recommended to use UIS if you wanna use the inputs ALL THE TIME. As for OP, check if it's a server script or a local script.

1 answer

If the error ServerScriptService.DefaultFPS.FPSMain:17: workspace.Script - attempt to index nil with - then dont worry it's happening to everyone. It does not do anything

2 comments

0

Why downvote :(

0

There is not line 17 in this script and this is hard to read and does not link with the problem, as OP did not specify an error.

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