Hello all, I’m sure you can interpret what I’m trying to do here. A basic binocular script. My question is how can I detect the mouse input after one section of the code has fired?
local UIS = game:GetService("UserInputService")
local Camera = workspace.CurrentCamera
local Tool = script.Parent
local TweenService = game:GetService("TweenService")
local goal = {}
goal.FieldOfView = 10
local tweenInfo = TweenInfo.new(1)
local tween = TweenService:Create(Camera, tweenInfo, goal)
UIS.InputBegan:Connect (function(Input)
if Input.UserInputType == Enum.UserInputType.MouseButton2 then
tween:Play()
end
end)
UIS.InputBegan:Connect (function(Input)
if Input.UserInputType == Enum.UserInputType.MouseButton2 then
Camera.FieldOfView = 70
end
end)
Currently it just does the first action twice instead of zooming in and then out. Also, on a separate note is there a more efficient way to create a teen?.. The way I did it looks a little more complex than it should be.
The issue is you’re running both blocks of code regardless of if the binoculars are already zoomed. You can use a bool (true/false) to keep track of this.
local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local Camera = workspace.CurrentCamera
local Tool = script.Parent
local goal = { -- No need to make this table and then instantly change it on the next line.
FieldOfView = 10
}
local tweenInfo = TweenInfo.new(1)
local tween = TweenService:Create(Camera, tweenInfo, goal)
local isZoomed = false
UIS.InputBegan:Connect(function(Input)
isZoomed = not isZoomed -- Toggles the boolean; If it's false, it becomes true, and if it's true, it becomes false.
if (Input.UserInputType == Enum.UserInputType.MouseButton2) and not (Tool.Parent:IsA("Backpack")) then -- If the tool isn't equipped, it shouldn't still zoom in if they right click.
if isZoomed then
tween:Play()
else
Camera.FieldOfView = 70
end
end
end)
If I understand your problem correctly: You’d like to tween the Camera’s FieldOfView property when MouseButton2 is pressed, and then set the property back to default when MouseButton2 is released. If so, this is what you need to do to achieve that result:
local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")
local currentCamera = workspace.CurrentCamera
local defaultFieldOfView = currentCamera.FieldOfView -- Since there's a chance it might change from 70 in the future
local tween = TweenService:Create(
currentCamera,
TweenInfo.new(1),
{FieldOfView = 10})
local function onInputBegan(input, gameProcessedEvent) -- Runs when MouseButton2 is pressed
if gameProcessedEvent then return end
if input.UserInputType == Enum.UserInputType.MouseButton2 then
tween:Play()
end
end
local function onInputEnded(input, gameProcessedEvent) -- Runs when MouseButton2 is released
if gameProcessedEvent then return end
if input.UserInputType == Enum.UserInputType.MouseButton2 then
tween:Cancel() -- Cancel the tween if it's still playing
currentCamera.FieldOfView = defaultFieldOfView
end
end
UserInputService.InputBegan:Connect(onInputBegan)
UserInputService.InputEnded:Connect(onInputEnded)
Although I noticed in the quote below that you seem to want to use this LocalScript inside of a Tool
The way your code is currently written, it will continue to work even if the Tool is not equipped. If this isn’t the behavior you desire, you’ll need to do this instead:
local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")
local tool = script.Parent
local currentCamera = workspace.CurrentCamera
local defaultFieldOfView = currentCamera.FieldOfView -- Since there's a chance it might change from 70 in the future
local tween = TweenService:Create(
currentCamera,
TweenInfo.new(1),
{FieldOfView = 10})
local inputBeganConnection, inputEndedConnection
local function onInputBegan(input, gameProcessedEvent) -- Runs when MouseButton2 is pressed
if gameProcessedEvent then return end
if input.UserInputType == Enum.UserInputType.MouseButton2 then
tween:Play()
end
end
local function onInputEnded(input, gameProcessedEvent) -- Runs when MouseButton2 is released
if gameProcessedEvent then return end
if input.UserInputType == Enum.UserInputType.MouseButton2 then
tween:Cancel() -- Cancel the tween if it's still playing
currentCamera.FieldOfView = defaultFieldOfView
end
end
local function onEquipped()
inputBeganConnection = UserInputService.InputBegan:Connect(onInputBegan)
inputEndedConnection = UserInputService.InputEnded:Connect(onInputEnded)
end
local function onUnequipped()
if inputBeganConnection then
inputBeganConnection:Disconnect()
inputBeganConnection = nil
end
if inputEndedConnection then
inputEndedConnection:Disconnect()
inputEndedConnection = nil
end
end
tool.Equipped:Connect(onEquipped)
tool.Unequipped:Connect(onUnequipped)
Also, one of the main reasons you’re experiencing the problem is because you accidentally connected to the InputBegan event twice instead of the InputBegan and InputEnded events
Thank you but the tool part is actually remains of some older code, this just goes in startercharcterscripts because it is always active.
No, I was not trying to make a hold down flashlight, I wanted a click to activate and then click again to deactivate flashlight. Thats why I attempted to put two begin inputs.
Thanks for the clarification on the table, despite the tool variable this wasn’t actually a tool, my bad it was an older code I am putting in startercharcterscripts. But after some tinkering I was able to get this and it works perfectly:
local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local Camera = workspace.CurrentCamera
local goal = {
FieldOfView = 10
}
local goal2 = {
FieldOfView = 70
}
local tweenInfo = TweenInfo.new(1)
local tween = TweenService:Create(Camera, tweenInfo, goal)
local tween2 = TweenService:Create(Camera, tweenInfo, goal2)
local isZoomed = false
UIS.InputBegan:Connect(function(Input)
isZoomed = not isZoomed -- Toggles the boolean; If it's false, it becomes true, and if it's true, it becomes false.
if (Input.UserInputType == Enum.UserInputType.MouseButton2) then
if isZoomed then
tween:Play()
else
tween2:Play()
end
end
end)
If there is a way to get around making two tweens let me know but otherwise I’m very pleased, thank you all.
If you prefer not to use TweenService, you can use a RunService loop and adjust the FOV using the deltaTime parameter returned. I noticed an important detail here:
The way your code is currently written, the isZoomed value will be toggled no matter what key or mouse button the player presses which will result in undesired behavior, so I fixed that problem as well:
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local SPEED = 1 -- In seconds it takes to reach target FOV
local GOAL_FOV = 10
local currentCamera = workspace.CurrentCamera
local defaultFieldOfView = currentCamera.FieldOfView
local x = defaultFieldOfView
local y = (defaultFieldOfView - GOAL_FOV) * (1 / SPEED)
local heartbeatConnection = nil
local function onInputBegan(input, gameProcessedEvent)
if gameProcessedEvent then return end
if input.UserInputType == Enum.UserInputType.MouseButton2 then
y = -y
if heartbeatConnection then return end
local clamp = math.clamp
local function onHeartbeat(deltaTime)
x = clamp(x + deltaTime * y, GOAL_FOV, defaultFieldOfView)
currentCamera.FieldOfView = x
if x == GOAL_FOV or x == defaultFieldOfView then
heartbeatConnection:Disconnect()
heartbeatConnection = nil
end
end
heartbeatConnection = RunService.Heartbeat:Connect(onHeartbeat)
end
end
UserInputService.InputBegan:Connect(onInputBegan)
You can conveniently adjust the SPEED and GOAL_FOV values to match what you desire