Hello! I am currently making a gun system, but i’ve ran into a issue. I want to make the player be able to scope / zoom in with their gun, but for some reason it isn’t working! Here is my code:
Mouse.Button2Down:Connect(function()
print("Down")
local tweenProperties = {FieldOfView = ScopeZoomAmount}
local zoomInfo = TweenInfo.new(ScopeInSpeed,Enum.EasingStyle.Sine,Enum.EasingDirection.Out,0,false,0)
local zoomTween = TS:Create(camera,zoomInfo,tweenProperties)
zoomTween:Play()
end)
Mouse.Button2Up:Connect(function()
local tweenProperties = {FieldOfView = 70}
local zoomInfo = TweenInfo.new(ScopeInSpeed,Enum.EasingStyle.Sine,Enum.EasingDirection.Out,0,true,0)
local zoomTween = TS:Create(camera,zoomInfo,tweenProperties)
zoomTween:Play()
end)
My print doesn’t work either, so it seems that Button2Down isn’t even running, and I have no clue why. This is a local script. Here is what each and every variable is set to.
local tool = script.Parent
local ScopeInSpeed = tool:GetAttribute("ScopeInSpeed")
local ScopeZoomAmount = tool:GetAttribute("ScopeZoomAmount")
local TS = game:GetService("TweenService")
local camera = game.Workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
My only thoughts are to make a remote event fire when the mouse is up or down and manage that in a server script, but I don’t think it would work because the MouseDown isn’t even running. Any help is apprciated!
I copied all your scripts and made it child of a tool (no handle, RequiresHandle set to false), and it did work.
There might be problem with your LocalScript’s parent, please provide the picture of localscripts position( explorer tab ) so that it would be easier to help
M4A1 is just under StarterPack. The code that I have listed above is in the LocalScript “GunClient”. Everything else works, but for some reason it is the only thing that doesn’t in that script.
Maybe your script has a while loop / infinite loop before the zooming script?
or … idk
Also this may be off-topic, but you should put a variable or a if statement that checks if the tool is equipped, so that it prevents zooming while you didnt equip the tool.
Mouse.Button2Down:Connect(function()
if tool.Parent ~= Player.Character then return end
print("Down")
local tweenProperties = {FieldOfView = ScopeZoomAmount}
local zoomInfo = TweenInfo.new(ScopeInSpeed,Enum.EasingStyle.Sine,Enum.EasingDirection.Out,0,false,0)
local zoomTween = TS:Create(camera,zoomInfo,tweenProperties)
zoomTween:Play()
end)
Mouse.Button2Up:Connect(function()
if tool.Parent ~= Player.Character then return end
local tweenProperties = {FieldOfView = 70}
local zoomInfo = TweenInfo.new(ScopeInSpeed,Enum.EasingStyle.Sine,Enum.EasingDirection.Out,0,true,0)
local zoomTween = TS:Create(camera,zoomInfo,tweenProperties)
zoomTween:Play()
end)
This script basically makes right click function only work when the tool is equipped.
-- Services
local players = game:GetService("Players")
local replicatedStrage = game:GetService("ReplicatedStorage")
-- Variables
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local tool = script.Parent
local remoteEvent = replicatedStrage.Gun
local CameraShaker = require(script:WaitForChild("CameraShaker"))
local camera = game.Workspace.CurrentCamera
local toolDebounce = false
local debounceTime = tool:GetAttribute("Cooldown")
local PrimaryWeapon = tool:GetAttribute("PrimaryWeapon")
local TargetIcon = 'rbxassetid://10883840867'
local NormalIcon = 'rbxassetid://7153417119'
local TS = game:GetService("TweenService")
local Recoil = tool:GetAttribute("Recoil")
local MouseHold = false
local ScopeInSpeed = tool:GetAttribute("ScopeInSpeed")
local ScopeZoomAmount = tool:GetAttribute("ScopeZoomAmount")
local tweenInfo = TweenInfo.new(0.025,Enum.EasingStyle.Elastic,Enum.EasingDirection.InOut,0,false,0)
local function recoil()
local oldCFrame = camera.CFrame
local tweenProperties = {CFrame = camera.CFrame * CFrame.Angles(Recoil,0,0)}
local recoilTween = TS:Create(camera,tweenInfo,tweenProperties)
recoilTween:Play()
end
tool.Equipped:Connect(function()
Mouse.Icon = TargetIcon
if PrimaryWeapon == true then
Player.CameraMode = Enum.CameraMode.LockFirstPerson
end
end)
tool.Unequipped:Connect(function()
Mouse.Icon = NormalIcon
if PrimaryWeapon == true then
Player.CameraMode = Enum.CameraMode.Classic
end
end)
Mouse.Button1Down:Connect(function()
MouseHold = true
end)
Mouse.Button1Up:Connect(function()
MouseHold = false
end)
while true do
if MouseHold then
if script.Parent.CurrentAmmo.Value <= 0 then
else
if not toolDebounce then
toolDebounce = true
local startPos = tool.Handle.Start.Position
local endPos = Mouse.Hit.Position
remoteEvent:FireServer(tool, startPos, endPos, Mouse)
recoil()
local camShake = CameraShaker.new(Enum.RenderPriority.Camera.Value, function(shakeCf)
camera.CFrame = camera.CFrame * shakeCf
end)
camShake:Start()
camShake:ShakeOnce(1,2,0.2,0.2)
task.wait(debounceTime)
toolDebounce = false
end
end
end
wait()
end
Mouse.Button2Down:Connect(function()
print("Down")
local tweenProperties = {FieldOfView = ScopeZoomAmount}
local zoomInfo = TweenInfo.new(ScopeInSpeed,Enum.EasingStyle.Sine,Enum.EasingDirection.Out,0,false,0)
local zoomTween = TS:Create(camera,zoomInfo,tweenProperties)
zoomTween:Play()
end)
Mouse.Button2Up:Connect(function()
local tweenProperties = {FieldOfView = 70}
local zoomInfo = TweenInfo.new(ScopeInSpeed,Enum.EasingStyle.Sine,Enum.EasingDirection.Out,0,true,0)
local zoomTween = TS:Create(camera,zoomInfo,tweenProperties)
zoomTween:Play()
end)
There is a while true loop to check if the mouse is being held down, but thats it. Should I rearrange anything to make it work?
Yeah… You are using while loop before the zoom script.
And to fix this, you should move your zoom script before the loop,
or use spawn(function() function to make a function doesnt affect the script after the loop.
spawn(function()
while wait(1) do
--do something
end
end)
print('GGGG')
and this DOES print “GGGG”, and It is because spawn function basically makes a new thread ( not really sure about what it does, ) and plays the function inside on that different thread, as I know.
and that thread now doesnt yield the current script so it’ll print “GGGG” even if it is on after the loop.