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

How do I make the mouse move freely when a GUI pops up?

I have this game that is always in FirstPerson mode, but when a GUI pops up the player is unable to click the gui.

Any fix?

wait(2)
local InputService=game:GetService("UserInputService")
local Camera=game.Workspace.CurrentCamera
local Player=game.Players.LocalPlayer
local Character=Player.Character
local Head=Character.Head
local Torso=Character.Torso
local RootPart=Character.HumanoidRootPart
local RootJoint=RootPart.RootJoint
local Neck=Torso.Neck
Camera.FieldOfView=90
Camera.CameraType="Scriptable"
InputService.MouseBehavior = Enum.MouseBehavior.LockCenter



local v3=Vector3.new
local cf=CFrame.new
local components=cf().components
local inverse=cf().inverse
local fromAxisAngle=CFrame.fromAxisAngle
local atan,atan2=math.atan,math.atan2
local acos=math.acos

local function toAxisAngleFromVector(v)
	local z=v.z
	return z*z<0.99999 and v3(v.y,-v.x,0).unit*acos(-z) or v3()
end

local function AxisAngleLookOrientation(c,v,t)--CFrame,Vector,Tween
	local c=c-c.p
	local rv=(inverse(c)*v).unit
	local rz=rv.z
	return rz*rz<0.99999 and c*fromAxisAngle(v3(rv.y,-rv.x,0),acos(-rz)*(t or 1)) or c
end

local function AxisAngleLookNew(v,t)--CFrame,Vector,Tween
	local rv=v.unit
	local rz=rv.z
	return rz*rz<0.99999 and fromAxisAngle(v3(rv.y,-rv.x,0),acos(-rz)*(t or 1)) or cf()
end

local function AxisAngleLook(c,v,t)--CFrame,Vector,Tween
	local rv=(inverse(c)*v).unit
	local rz=rv.z
	return rz*rz<0.99999 and c*fromAxisAngle(v3(rv.y,-rv.x,0),acos(-rz)*(t or 1)) or c
end




local Sensitivity=0.005


local CameraDirection=Vector3.new(0,0,1)

local function EulerAnglesYX(l)
	local x,z=l.x,l.z
	return atan(l.y/(x*x+z*z)^0.5),-atan2(x,-z)
end

local function AnglesXY(l)
	local z=l.z
	return atan2(l.y,-z),-atan2(l.x,-z)
end

local function MouseMoved(Input)
	if Input.UserInputType==Enum.UserInputType.MouseMovement then
		local dx,dy=Input.Delta.x*Sensitivity,Input.Delta.y*Sensitivity
		local m2=dx*dx+dy*dy
		if m2>0 then
			CameraDirection=(AxisAngleLookOrientation(RootPart.CFrame,CameraDirection)*fromAxisAngle(v3(-dy,-dx,0),m2^0.5)).lookVector
		end
		local RootOrientation=RootPart.CFrame-RootPart.Position
		local RelativeDirection=RootOrientation:inverse()*CameraDirection
		local AngX,AngY=AnglesXY(RelativeDirection)--RootOrientation:inverse()*
		if AngX<-1.57*11/12 then
			local y,z,c,s=RelativeDirection.y,RelativeDirection.z,math.cos(-1.57*11/12-AngX),-math.sin(-1.57*11/12-AngX)
			z,y=z*c-y*s,z*s+y*c
			CameraDirection=RootOrientation*v3(RelativeDirection.x<0 and -(1-y*y-z*z)^0.5 or (1-y*y-z*z)^0.5,y,z)
		elseif AngX>1.57*11/12 then
			local y,z,c,s=RelativeDirection.y,RelativeDirection.z,math.cos(1.57*11/12-AngX),-math.sin(1.57*11/12-AngX)
			z,y=z*c-y*s,z*s+y*c
			CameraDirection=RootOrientation*v3(RelativeDirection.x<0 and -(1-y*y-z*z)^0.5 or (1-y*y-z*z)^0.5,y,z)
		end
	end
end

local Mouse=Player:GetMouse()

local Zoom=-0.5

Mouse.KeyDown:connect(function(k) 
	if k=="e" then 	
		Zoom=-0.5
	elseif k=="q" then
		Zoom=-0.5
	end
end)

InputService.InputChanged:connect(MouseMoved)

Neck.C1=cf()

local _
local DirectionBound=3.14159/3
local CurrentAngY=0

local function CameraUpdate()
	Camera.CameraType="Scriptable"
	local cx,cz=CameraDirection.x,CameraDirection.z
	local rvx,rvz=RootPart.Velocity.x,RootPart.Velocity.z
	if rvx*rvx+rvz*rvz>4 and cx*rvx+cz*rvz<-0.5*(cx*cx+cz*cz)^0.5*(rvx*rvx+rvz*rvz)^0.5 then
		DirectionBound=math.min(DirectionBound*0.9,math.abs(CurrentAngY*0.9))
	else
		DirectionBound=DirectionBound*0.1+3.14159/3*0.9
	end
	local AngX,AngY=EulerAnglesYX((RootPart.CFrame-RootPart.Position):inverse()*CameraDirection)
	if AngY>DirectionBound then
		RootPart.CFrame=RootPart.CFrame*CFrame.Angles(0,AngY-DirectionBound,0)
	elseif AngY<-DirectionBound then
		RootPart.CFrame=RootPart.CFrame*CFrame.Angles(0,AngY+DirectionBound,0)
	end
	_,CurrentAngY=EulerAnglesYX((RootPart.CFrame-RootPart.Position):inverse()*CameraDirection)
	local CameraOrientation=AxisAngleLookNew((RootPart.CFrame-RootPart.Position):inverse()*CameraDirection,1)
	Neck.C0=CFrame.new(0,1,0)*CameraOrientation*CFrame.new(0,0.5,0)
	local PreCam=AxisAngleLook(RootPart.CFrame*cf(0,1,0),RootPart.CFrame*v3(0,1,0)+CameraDirection)*CFrame.new(0,0.825,0)
	if Zoom==8 then
		local Part,Position=Workspace:findPartOnRay(Ray.new(PreCam.p,PreCam.lookVector*-8),Character)
		Camera.CoordinateFrame=PreCam*CFrame.new(0,0,(Position-PreCam.p).magnitude)
	else
		Camera.CoordinateFrame=PreCam*CFrame.new(0,0,Zoom)
	end
end

game:GetService("RunService").RenderStepped:connect(CameraUpdate)

6 comments

0

You can send the script in code box?

0

There it is.

0

Did you do anything about GUI detection? Or not?

0

Not yet.

0

Is there any reason you can't just hook allowing mouse movement to GetPropertyChangedSignal() on GuiObject's Visible? Also - all of these mathematical functions would serve better from a module imo.. kinda clogs this script up.

0

I don't think anything should stop it, and yeah, I realized that..

1 answer

✓ Accepted answer

I've been looking for what to do. So I had an idea:

Create a BoolValue in the GUI called "PopUp" When your GUI appears, leave PopUp true. After the GUI disappears, leave PopUp false.

You should only add the GUI name in the script. If you need more than 1 GUI to move the mouse tell me in comments. i will try to fix for more than 1 GUI.

Fixed script:

wait(2)

local ValueLocation = script.Parent -- Find PlayerGui/Location of all GUI's
local ValueName = "PopUp"

if ValueLocation:FindFirstChild(ValueName) then
	print("Already have a instance.")
else
	local instance = Instance.new("BoolValue", ValueLocation)
	instance.Name = ValueName
	print("Instance created! if you set this value to true. can move mouse! ( INFO: Instance Name" .. tostring(ValueName) .. " Instance Parent: " .. tostring(ValueLocation.Name) .. " )")
end

local InputService=game:GetService("UserInputService")
local Camera=game.Workspace.CurrentCamera
local Player=game.Players.LocalPlayer
local Character=Player.Character
local Head=Character.Head
local Torso=Character.Torso
local RootPart=Character.HumanoidRootPart
local RootJoint=RootPart.RootJoint
local Neck=Torso.Neck
Camera.FieldOfView=90
Camera.CameraType="Scriptable"
InputService.MouseBehavior = Enum.MouseBehavior.LockCenter



local v3=Vector3.new
local cf=CFrame.new
local components=cf().components
local inverse=cf().inverse
local fromAxisAngle=CFrame.fromAxisAngle
local atan,atan2=math.atan,math.atan2
local acos=math.acos

local function toAxisAngleFromVector(v)
    local z=v.z
    return z*z<0.99999 and v3(v.y,-v.x,0).unit*acos(-z) or v3()
end

local function AxisAngleLookOrientation(c,v,t)--CFrame,Vector,Tween
    local c=c-c.p
    local rv=(inverse(c)*v).unit
    local rz=rv.z
    return rz*rz<0.99999 and c*fromAxisAngle(v3(rv.y,-rv.x,0),acos(-rz)*(t or 1)) or c
end

local function AxisAngleLookNew(v,t)--CFrame,Vector,Tween
    local rv=v.unit
    local rz=rv.z
    return rz*rz<0.99999 and fromAxisAngle(v3(rv.y,-rv.x,0),acos(-rz)*(t or 1)) or cf()
end

local function AxisAngleLook(c,v,t)--CFrame,Vector,Tween
    local rv=(inverse(c)*v).unit
    local rz=rv.z
    return rz*rz<0.99999 and c*fromAxisAngle(v3(rv.y,-rv.x,0),acos(-rz)*(t or 1)) or c
end

local Sensitivity=0.005
local CameraDirection=Vector3.new(0,0,1)

local function EulerAnglesYX(l)
    local x,z=l.x,l.z
    return atan(l.y/(x*x+z*z)^0.5),-atan2(x,-z)
end

local function AnglesXY(l)
    local z=l.z
    return atan2(l.y,-z),-atan2(l.x,-z)
end

local function MouseMoved(Input)
    if Input.UserInputType==Enum.UserInputType.MouseMovement then
        local dx,dy=Input.Delta.x*Sensitivity,Input.Delta.y*Sensitivity
        local m2=dx*dx+dy*dy
        if m2>0 then
            CameraDirection=(AxisAngleLookOrientation(RootPart.CFrame,CameraDirection)*fromAxisAngle(v3(-dy,-dx,0),m2^0.5)).lookVector
        end
        local RootOrientation=RootPart.CFrame-RootPart.Position
        local RelativeDirection=RootOrientation:inverse()*CameraDirection
        local AngX,AngY=AnglesXY(RelativeDirection)--RootOrientation:inverse()*
        if AngX<-1.57*11/12 then
            local y,z,c,s=RelativeDirection.y,RelativeDirection.z,math.cos(-1.57*11/12-AngX),-math.sin(-1.57*11/12-AngX)
            z,y=z*c-y*s,z*s+y*c
            CameraDirection=RootOrientation*v3(RelativeDirection.x<0 and -(1-y*y-z*z)^0.5 or (1-y*y-z*z)^0.5,y,z)
        elseif AngX>1.57*11/12 then
            local y,z,c,s=RelativeDirection.y,RelativeDirection.z,math.cos(1.57*11/12-AngX),-math.sin(1.57*11/12-AngX)
            z,y=z*c-y*s,z*s+y*c
            CameraDirection=RootOrientation*v3(RelativeDirection.x<0 and -(1-y*y-z*z)^0.5 or (1-y*y-z*z)^0.5,y,z)
        end
    end
end

local Mouse=Player:GetMouse()

local Zoom=-0.5

Mouse.KeyDown:connect(function(k) 
    if k=="e" then  
        Zoom=-0.5
    elseif k=="q" then
        Zoom=-0.5
    end
end)

InputService.InputChanged:connect(MouseMoved)

Neck.C1=cf()

local _
local DirectionBound=3.14159/3
local CurrentAngY=0

local function CameraUpdate()
	    if ValueLocation:FindFirstChild(ValueName).Value == false then
	        if InputService.MouseBehavior ~= Enum.MouseBehavior.LockCenter then
	            InputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	            Character.Humanoid.WalkSpeed = 16
	        end
	        Camera.CameraType="Scriptable"
	        local cx,cz=CameraDirection.x,CameraDirection.z
	        local rvx,rvz=RootPart.Velocity.x,RootPart.Velocity.z
	        if rvx*rvx+rvz*rvz>4 and cx*rvx+cz*rvz<-0.5*(cx*cx+cz*cz)^0.5*(rvx*rvx+rvz*rvz)^0.5 then
	            DirectionBound=math.min(DirectionBound*0.9,math.abs(CurrentAngY*0.9))
	        else
	            DirectionBound=DirectionBound*0.1+3.14159/3*0.9
	        end
	        local AngX,AngY=EulerAnglesYX((RootPart.CFrame-RootPart.Position):inverse()*CameraDirection)
	        if AngY>DirectionBound then
	            RootPart.CFrame=RootPart.CFrame*CFrame.Angles(0,AngY-DirectionBound,0)
	        elseif AngY<-DirectionBound then
	            RootPart.CFrame=RootPart.CFrame*CFrame.Angles(0,AngY+DirectionBound,0)
	        end
	        _,CurrentAngY=EulerAnglesYX((RootPart.CFrame-RootPart.Position):inverse()*CameraDirection)
	        local CameraOrientation=AxisAngleLookNew((RootPart.CFrame-RootPart.Position):inverse()*CameraDirection,1)
	        Neck.C0=CFrame.new(0,1,0)*CameraOrientation*CFrame.new(0,0.5,0)
	        local PreCam=AxisAngleLook(RootPart.CFrame*cf(0,1,0),RootPart.CFrame*v3(0,1,0)+CameraDirection)*CFrame.new(0,0.825,0)
	        if Zoom==8 then
	            local Part,Position=workspace:findPartOnRay(Ray.new(PreCam.p,PreCam.lookVector*-8),Character)
	             Camera.CoordinateFrame=PreCam*CFrame.new(0,0,(Position-PreCam.p).magnitude)
	        else
	            Camera.CoordinateFrame=PreCam*CFrame.new(0,0,Zoom)
	        end
	    elseif ValueLocation:FindFirstChild(ValueName).Value == true then
	       InputService.MouseBehavior = Enum.MouseBehavior.Default
	       Character.Humanoid.WalkSpeed = 0
	end
end

game:GetService("RunService").RenderStepped:connect(CameraUpdate)

5 comments

0

More than one gui.

0

But this will work for now, thank you.

0

When I edit, i warn in the comments here.

0

I edited now.

0

So, I guess it's not necessary to create multiple values called PopUp in GUI, just creating one in the PlayerGUI would be enough. then you can use the edited script. and every time a GUI appears you'd leave it to true. instead of creating on GUI boos this would support several, even because it is only set to true and ready...

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