How do I close a function?

I have no idea why the hell my script decides to error. It says close the function, but I don’t know how and I didn’t delete or remove anything.

edit: to specify, the last “end” has a red underline for no reason.

local scoped = false
local cam

script.Parent.Equipped:connect(function(mouse)
cam = game.Workspace.CurrentCamera
mouse.KeyDown:connect(function(key)
key = key:lower()

	if key == "q" then
		cam.FieldOfView = scoped and 70 or 50
		scoped = not scoped
	
		
		
		script.Parent.MaxSpread.Value = 0.025
		
		script.Parent.ADSModel.ADSBarrelA.Transparency = 0
		script.Parent.ADSModel.FakeADSM.Transparency = 0
		script.Parent.ADSModel.ADSBackSights.Transparency = 0
		script.Parent.ADSModel.ADSGun.Transparency = 0
		script.Parent.ADSModel.ADSSight1.Transparency = 0
		script.Parent.ADSModel.ADSSight2.Transparency = 0
		script.Parent.ADSModel.BulletA1.Transparency = 0
		script.Parent.ADSModel.BulletA2.Transparency = 0
		script.Parent.ADSModel.BulletA3.Transparency = 0
		script.Parent.ADSModel.BulletA4.Transparency = 0
		script.Parent.ADSModel.BulletA5.Transparency = 0
		script.Parent.ADSModel.BulletA6.Transparency = 0
		script.Parent.ADSModel.BulletA7.Transparency = 0
		script.Parent.ADSModel.BulletA8.Transparency = 0

end

script.Parent.Unequipped:connect(function()

cam.FieldOfView = 70

script.Parent.MaxSpread.Value = 0.1
			


end
2 Likes

try placing a “)” next to the d in “end”? I think you forgot to close the connect

i did, but it didn’t work, that’s why i made this post

Indent properly so that you can tell code blocks apart and find out where ends may be missing.
When you use anonymous functions to declare event listeners, those need to be closed.

local scoped = false
local cam

script.Parent.Equipped:connect(function(mouse)
	cam = game.Workspace.CurrentCamera
	mouse.KeyDown:Connect(function(key) -- 'connect' is deprecated, use 'Connect'
		key = key:lower()
		if key == "q" then
			cam.FieldOfView = scoped and 70 or 50
			scoped = not scoped
			script.Parent.MaxSpread.Value = 0.025
			script.Parent.ADSModel.ADSBarrelA.Transparency = 0
			script.Parent.ADSModel.FakeADSM.Transparency = 0
			script.Parent.ADSModel.ADSBackSights.Transparency = 0
			script.Parent.ADSModel.ADSGun.Transparency = 0
			script.Parent.ADSModel.ADSSight1.Transparency = 0
			script.Parent.ADSModel.ADSSight2.Transparency = 0
			script.Parent.ADSModel.BulletA1.Transparency = 0
			script.Parent.ADSModel.BulletA2.Transparency = 0
			script.Parent.ADSModel.BulletA3.Transparency = 0
			script.Parent.ADSModel.BulletA4.Transparency = 0
			script.Parent.ADSModel.BulletA5.Transparency = 0
			script.Parent.ADSModel.BulletA6.Transparency = 0
			script.Parent.ADSModel.BulletA7.Transparency = 0
			script.Parent.ADSModel.BulletA8.Transparency = 0
		end
	end) -- added
	script.Parent.Unequipped:Connect(function() -- again here
		cam.FieldOfView = 70
		script.Parent.MaxSpread.Value = 0.1
	end) --added		
end) -- added closing parentheses
2 Likes

Also, it’s generally a bad idea to connect to events inside other event listeners.

You either need to disconnect the events when you’re done with them (e.g. inside Unequipped), or disable their processing with some flag variable (e.g. make every connection fully separate, and use global vars to control when the functions should execute.)

1 Like

oh oof. Wish you luck(bc I don’t rlly know what to do xD)

Is it that bad? I find it easier to read sometimes, and you don’t have to scroll and stuff, what about events that you DON’T need to disconnect?

What he means is that every time the tool is equipped it will create NEW events that are unnecessary since there are ones that already exist after the first equip.

2 Likes

Not only unnecessary, but in this case they’ll trigger when you expect them not to.