Help on gun script review (very messy code)

not satifsified with any of this code

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local Gun = script.Parent.Parent:FindFirstChild('Gun')

local UserInputService = game:GetService('UserInputService')
local TweenService = game:GetService('TweenService')
local Camera = workspace.CurrentCamera
local ReplicatedStorage = game:GetService('ReplicatedStorage')

local Index = {['ZoomIn'] = 60, ['ZoomOut'] = 70}
local Info = TweenInfo.new(0.21, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)


local function TweenCamera(Properties)
	local T = TweenService:Create(game.Workspace.CurrentCamera, Info, Properties)
	T:Play()
end


local equipped = false
local function Zoom(key)
	if Player and Player.Character then
		UserInputService.InputBegan:Connect(function(input)
			if input.UserInputType == Enum.UserInputType[key] then
				if equipped then
				local Properties = {FieldOfView = Index.ZoomIn}
                TweenCamera(Properties)
                Mouse.Icon = ''

                local Animation = Instance.new('Animation')
			    Animation.AnimationId = 'rbxassetid://4842699362'
			
			    local AnimLoad = Player.Character.Humanoid:LoadAnimation(Animation)
			    AnimLoad:Play()
	
			AnimLoad.Stopped:Connect(function()
				local HoldingAnimation = Instance.new('Animation')
				HoldingAnimation.AnimationId = 'rbxassetid://04842792191'
				local Track = Player.Character.Humanoid:LoadAnimation(HoldingAnimation)
				Track.Looped = true
				
				Track:Play()
				end)
		           
		        end
		   end
			
					end)
						UserInputService.InputEnded:Connect(function(input)
								if equipped then
					      local Properties = {FieldOfView = Index.ZoomOut}
					      TweenCamera(Properties)
					
					for i,tracks in ipairs(Player.Character.Humanoid:GetPlayingAnimationTracks()) do
						tracks:Stop()
						end
					wait(0.1)
					Mouse.Icon = 'rbxassetid://61374156'
					end
					end)
						end
						end
			
					
            
		 Gun.Equipped:Connect(function()
			equipped = true
			Zoom('MouseButton2')
			Mouse.Icon = 'rbxassetid://61374156'
		end)
		
		
	    
		Gun.Unequipped:Connect(function()
			local Properties = {FieldOfView = Index.ZoomOut}
			TweenCamera(Properties)
			equipped = false
			Mouse.Icon = ''
		end)

needs improving?

5 Likes

A good start is always making sure the code is readable and clear, you can achieve this in many ways but the easiest is probably indenting and then commenting your code. You are also creating a new event each time the tool is equipped which isn’t really recommended.

After all of this and some other tweaking we end up with this:

local UserInputService = game:GetService('UserInputService')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local TweenService = game:GetService('TweenService')
local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

local Gun = script.Parent.Parent:WaitForChild('Gun')
local Camera = Workspace.CurrentCamera

local Index = {
	['ZoomIn'] = 60,
	['ZoomOut'] = 70
}

local Info = TweenInfo.new(0.21, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)

local function TweenCamera(Properties)
	local T = TweenService:Create(game.Workspace.CurrentCamera, Info, Properties)
	T:Play()
end

local equipped = false

local function ZoomIn()
	local Properties = {
		FieldOfView = Index.ZoomIn;
	}
	
	TweenCamera(Properties)
end

local function ZoomOut()
	local Properties = {
		FieldOfView = Index.ZoomOut;
	}
	
	TweenCamera(Properties)
end

local function StoppedAnimation()
	local HoldingAnimation = Instance.new('Animation')
	HoldingAnimation.AnimationId = 'rbxassetid://04842792191'
	local Track = Player.Character.Humanoid:LoadAnimation(HoldingAnimation)
	Track.Looped = true
	
	Track:Play()
end

local Stopped
local function Listen()
	
	UserInputService.InputBegan:Connect(function(input)
		if input == Enum.UserInputType.MouseButton2 then
			if equipped and Player and Player.Character then
				
				-- Disconnect stopped
				if (Stopped) then
					Stopped:Disconnect()
					Stopped = nil
				end
				
				-- Zoom in
				ZoomIn()

				-- Reset mouse icon
				Mouse.Icon = ''

				-- Create animation
				local Animation = Instance.new('Animation')
				Animation.AnimationId = 'rbxassetid://4842699362'
		
				-- Load animation
				local AnimLoad = Player.Character.Humanoid:LoadAnimation(Animation)
				AnimLoad:Play()
				
				-- Connect stopped listener
				Stopped = AnimLoad.Stopped:Connect(function()
					
					-- Run stopped animation
					StoppedAnimation()

					-- Disconnect stopped listener
					Stopped:Disconnect()
					Stopped = nil
				end)
				
			end
		end
	end)
		
	UserInputService.InputEnded:Connect(function(input)
		if equipped and Player and Player.Character then
			
			-- Disconnect stopped
			if (Stopped) then
				Stopped:Disconnect()
				Stopped = nil
			end
			
			-- Run stopped animation
			StoppedAnimation()
			
			local Properties = {
				FieldOfView = Index.ZoomOut
			}
			
			-- Tween camera
			TweenCamera(Properties)
			
			-- Stop animations
			for i, tracks in ipairs(Player.Character.Humanoid:GetPlayingAnimationTracks()) do
				tracks:Stop()
			end
			
			wait(0.1)
			Mouse.Icon = 'rbxassetid://61374156'
		end
	end)
	
end

-- Connect UserInputService listener
Listen()

-- Connect Equipped Listener
Gun.Equipped:Connect(function()
	
	-- Set equipped to true
	equipped = true
	
	-- Set mouse icon
	Mouse.Icon = 'rbxassetid://61374156'
	
end)

-- Connect Unequipped Listener
Gun.Unequipped:Connect(function()
	
	-- Zoom out
	ZoomOut()
	
	-- Set equipped to false
	equipped = false
	
	-- Reset mouse icon
	Mouse.Icon = ''
	
end)

I did not test this code so if you do experience any problems please let me know.

2 Likes