How Would I add Scope to a gun?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want to add a scope feature to my M4A1 gun.

  1. What is the issue? Include screenshots / videos if possible!

I have no idea how to add scope to my gun, as it is custom system.

I have a part I want the camera to “tween” to.
image
Called Scope.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Yes, but none were compatible with my gun system.

Here, I will present you with some code.

This code, is the code that will have the scope in function. This is in the client sided script inside of the firearm.

Mouse.Button2Down:Connect(function()
	if GloballyEnabled == true then
		-- scope function
	end
end)

GloballyEnabled is a BooLean value that is true when the tool is equipped, false when it’s not.

How I want it to be:

Sorry for OBS lag.

Here, I will present a game link.

4 Likes

Any help would be appreciated. I have no experience in stuff involving the camera.

Here is the selected part that is the scope destination.

4 Likes

You could try adding a tween to set the player’s camera’s CFrame to the part’s CFrame. You can get the player’s camera by using workspace.CurrentCamera, if it’s a local script. Make sure to set the camera type to scriptable in the script so that it will actually move the camera.

2 Likes

Ok, I will try this in a second

1 Like

Heres a code i made in less than 5 minutes, It shoud move your camera to the part and back to your character when you release MB2

-- in localscript

local CM = game.Workspace.CurrentCamera
local PRT = game.Workspace:WaitForChild("CP") --scope part path here
local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")
local SCOPE_TWEENS = {
	["TWEEN_OPEN"] = TweenInfo.new(0.45,Enum.EasingStyle.Quart,Enum.EasingDirection.Out), -- the tween for when the camera enters the scope
	["TWEEN_CLOSE"] = TweenInfo.new(0.45,Enum.EasingStyle.Quart,Enum.EasingDirection.Out), -- the tween for when the camera leaves the scope
}

local GloballyEnabled = true -- testing\

local ScopeFOCUSED = false

UIS.InputBegan:Connect(function(INP,PROC)
	if PROC then return end -- prevent accidental scope focus eg: moving camera
	if INP.UserInputType == Enum.UserInputType.MouseButton2 then
		warn("MB2CLICKSTARTED") -- debug, remove in production
		if GloballyEnabled == true then
			ScopeFOCUSED = true
			CM.CameraType = Enum.CameraType.Scriptable
			CM.CameraSubject = PRT
			TS:Create(CM,SCOPE_TWEENS["TWEEN_OPEN"],{
				CFrame = PRT.CFrame
			}):Play()

		end
	end
end)

UIS.InputEnded:Connect(function(INP)
	if INP.UserInputType == Enum.UserInputType.MouseButton2 then
		warn("MB2CLICKENDED") -- debug, remove in production
		if GloballyEnabled == true then
			if ScopeFOCUSED == true then
				ScopeFOCUSED = false
				CM.CameraType = Enum.CameraType.Custom
				CM.CameraSubject = game.Players.LocalPlayer.Character:FindFirstChildWhichIsA("Humanoid")
				TS:Create(CM,SCOPE_TWEENS["TWEEN_CLOSE"],{
					CFrame = game.Players.LocalPlayer.Character:FindFirstChild("Head").CFrame
				}):Play()
			end
		end
	end
end)

Note: i did not code in the scope image code, that should be easy to code so ill allow you to code that.

5 Likes

Ok I used the code you gave me, but it errored
image
on this line


and this line
image

3 Likes

ok I fixed it replacing with

local t = TS:Create(CMM,SCOPE_TWEENS["TWEEN_CLOSE"],{CFrame = game.Players.LocalPlayer.Character:FindFirstChild("Head").CFrame})
				t:Play()

instead

1 Like

add this to prevent the tween from causing lag considering your using local values for it now

t.Completed:Connect(function()
					t:Destroy()
				end)
2 Likes

Ok on your system, I notice that, it tweens to the guns scope, but the camera is fixed to its cframe, and I can’t move the camera around either.

I want a gun scope like this:

1 Like

Also, I did not understand how to do this and make it stay on scope position.

Help me I need help on gun scope this is only thing I have left for add its not that important to me but still scope is main part of gun to improve accuracy

Adding a scope feature to your M4A1 gun in Roblox involves manipulating the camera’s properties to provide a scoped view when the right mouse button is pressed. To achieve this, you can follow these steps:

  1. Create the Scope:

    • In your gun’s model, add a part called “Scope” to represent the scope’s frame.
    • Position the “Scope” part appropriately to match where the camera should focus when scoped.
  2. Modify Your Script:

    • You can use the UserInputService to control the camera’s properties when the right mouse button is pressed.

Here’s how you can modify your script:

local UserInputService = game:GetService("UserInputService")

-- Assuming you have a reference to your gun's model and the "Scope" part
local gunModel = -- your gun model
local scopePart = gunModel:FindFirstChild("Scope")

local isScoped = false  -- A flag to track whether the player is scoped

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if gameProcessedEvent then
        return  -- Ignore input events that have been processed by Roblox
    end
    
    if input.UserInputType == Enum.UserInputType.MouseButton2 and GloballyEnabled then
        isScoped = not isScoped  -- Toggle the scoped state

        if isScoped then
            -- Enter scoped view
            workspace.CurrentCamera.CFrame = CFrame.new(scopePart.Position, gunModel.PrimaryPart.Position)
            -- You may need to adjust the position and rotation for a proper scoped view
        else
            -- Exit scoped view
            workspace.CurrentCamera.CFrame = CFrame.new(gunModel.PrimaryPart.Position + Vector3.new(0, 5, 10), gunModel.PrimaryPart.Position)
            -- Adjust the position for a non-scoped view
        end
    end
end)

In this script, we use UserInputService to detect when the right mouse button is pressed. When it’s pressed, we toggle the scoped state (isScoped) and adjust the camera’s CFrame accordingly to simulate the scoped and non-scoped views.

You may need to fine-tune the CFrame positions and rotations to achieve the desired scoped and non-scoped views. Additionally, adjust the scopePart.Position and camera position in the scoped view to fit your specific gun model and scope design.

Please make sure to replace gunModel with your gun’s actual reference.

1 Like

I did everything and set primary part and set variables but it snaps my camera like this on mousebutton2

help … . mee. . . . … . i am slowly losing hope . . . . . …

you can use Camera.FieldOfView. Set the value to around 30 and it should work like a scope.

1 Like

I could do that but it also isn’t that realistic but I’ll do that for now