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.
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.
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:
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.
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.