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

Function inside OnEquipped function stills works after unequipping tool?

I'm trying to make it so that the camera zooms in when the player presses the right mouse button while the tool is equipped. It works for the most part, but the problem is it still works even after I have unequipped the tool. I have a very loose understanding on functions, though if I had to guess, it's because of the mouse connect function within the ADS function. Or maybe there is a way to stop the function in the OnUnequipped function?

function ADS()
	local aiming = false
	local mouse = game.Players.LocalPlayer:GetMouse()
	mouse.Button2Down:connect(function()
		aiming = true
		for i = 1,5 do
			game.Workspace.CurrentCamera.FieldOfView = (70-(i*2))
			wait()
		end
		repeat wait () until aiming == false
		end)
	mouse.Button2Up:connect(function()
		aiming = false
		for i = 1,5 do
			game.Workspace.CurrentCamera.FieldOfView = (60+(i*2))
			wait()
		end
	end)
end

function OnEquipped(playerMouse)
	ADS()
end

1 comment

0

I found several problems, fixing

1 answer

✓ Accepted answer

I found many problems with your ADS script.

This is the final revised script

local camera = workspace.CurrentCamera
local ts = game:GetService("TweenService")
local zoomintween = ts:Create(camera, TweenInfo.new(0.15), {FieldOfView = 60})
local zoomouttween = ts:Create(camera, TweenInfo.new(0.15), {FieldOfView = 70})
local tool = script.Parent
local adsin, adsout

function zoomin()
	if zoomouttween.PlaybackState == Enum.PlaybackState.Playing then
		zoomouttween:Cancel()
	end
	zoomintween:Play()
end

function zoomout()
	if zoomintween.PlaybackState == Enum.PlaybackState.Playing then
		zoomintween:Cancel()
	end
	zoomouttween:Play()
end

function ads(mouse)
	adsin = mouse.Button2Down:Connect(zoomin)
	adsout = mouse.Button2Up:Connect(zoomout)
end

function unequipped()
	adsin:Disconnect()
	adsout:Disconnect()
end

tool.Equipped:Connect(ads)
tool.Unequipped:Connect(unequipped)

Your first problem is that you didn't connect the tool equipping to the actual event. This was fixed in my script.

Your second problem is that your tweens weren't smooth. This was solved with TweenService.

Your third problem is that sometimes, your tween could overlap each other. This was solved by detecting if the other tween was playing, and if so, canceling it.

Your fourth problem is that you didn't disconnect the event afterwards.

1 comment

0

legend

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